springboot使用proguard代碼混淆配置,解決混淆后掃描不到無法注入的問題


解決無法注入的問題

打包之后注入提示

required a bean of type 'com.xxx.xxx' that could not be found

查詢各種資料,研究了半天,一個配置就搞定
添加

<!-- 保持目錄結構-->
<option>-keepdirectories</option>

完整配置

pom.xml文件中添加入下配置

    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <!--代碼混淆插件-->
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>6.2.2</proguardVersion>
                    <injar>${project.build.finalName}.jar</injar>
                    <outjar>${project.build.finalName}.jar</outjar>
                    <obfuscate>true</obfuscate>
                    <options>
                        <option>-target 1.8</option>
                        <!--  ##默認是開啟的,這里關閉shrink,即不刪除沒有使用的類/成員-->
                        <option>-dontshrink</option>
                        <!-- ##默認是開啟的,這里關閉字節碼級別的優化-->
                        <option>-dontoptimize</option>
                        <!--##對於類成員的命名的混淆采取唯一策略-->
                        <option>-useuniqueclassmembernames</option>
                        <!--- 混淆類名之后,對使用Class.forName('className')之類的地方進行相應替代-->
                        <option>-adaptclassstrings</option>
                        <option>-ignorewarnings</option>
                        <!-- 混淆時不生成大小寫混合的類名,默認是可以大小寫混合-->
                        <option>-dontusemixedcaseclassnames</option>
                        <!-- This option will replace all strings in reflections method invocations with new class names.
                             For example, invokes Class.forName('className')-->
                        <!-- <option>-adaptclassstrings</option> -->
                        <!-- This option will save all original annotations and etc. Otherwise all we be removed from files.-->
                        <!-- 不混淆所有特殊的類-->
                        <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                            SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
                        </option>
                        <!-- This option will save all original names in interfaces (without obfuscate).-->
                        <option>-keepnames interface **</option>
                        <!-- This option will save all original methods parameters in files defined in -keep sections,
                             otherwise all parameter names will be obfuscate.-->
                        <option>-keepparameternames</option>
                        <!-- 保持目錄結構-->
                        <option>-keepdirectories</option>

                        <!-- 排除config文件夾-->
                        <option>-keep class com.hrms.framework.config.* { *; }</option>

                        <option>-keep class * implements java.io.Serializable</option>

                        <option>-keep interface * extends * { *; }</option>

                        <!-- 排除所有public class 和 method-->
                        <option>-keep public class * { public protected *; }
                        </option>
                        <!-- <option>-keep @org.springframework.stereotype.Service class *</option> -->
                        <!-- This option will save all original defined annotations in all class in all packages.-->
                        <option>-keepclassmembers class * {
                            <!-- @org.springframework.beans.factory.annotation.Autowired *; -->
                            @org.springframework.beans.factory.annotation.Value *;
                            }
                        </option>
                    </options>
                    <libs>
                        <!-- Include main JAVA library required.-->
                        <lib>${java.home}/lib/rt.jar</lib>
                        <!-- Include crypto JAVA library if necessary.-->
                        <lib>${java.home}/lib/jce.jar</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>6.2.2</version>
                    </dependency>
                </dependencies>

            </plugin>


        </plugins>
    </build>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM