springboot2.x+maven+proguard代码混淆

时间:2021-11-28 17:17:52

由于需要将源码打包做代码混淆,选择proguard,开始使用各种问题,各种jar包版本问题,但最终成功了,记录一下,也希望能够帮助大家

在pom中添加代码:

    <build>
        <finalName>${artifactId}</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.0.3</proguardVersion>
                    <injar>${project.build.finalName}.jar</injar>
                    <!-- <injar>classes</injar> -->
                    <outjar>${project.build.finalName}.jar</outjar>
                    <obfuscate>true</obfuscate>
                    <options>
                        <!-- 不做收缩(删除注释、未被引用代码)-->
                        <option>-dontshrink</option>
                        <!-- 不做优化(变更代码实现逻辑)-->
                        <option>-dontoptimize</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>-libraryjars **</option> -->
                        <!-- This option will save all original class files (without obfuscate) but obfuscate all in domain package.-->
                        <!--<option>-keep class !com.slm.proguard.example.spring.boot.domain.** { *; }</option>-->
                        <option>-keep class !com.ts.** { *; }</option>
                        <option>-keep class com.ts.Application { *; }</option>
                        <option>-keep class com.ts.SwaggerConfig { *; }</option>
                        <option>-keep class com.ts.common.** { *; }</option>
                        <option>-keep class com.ts.aop.** { *; }</option>
                        <option>-keep class com.ts.config.** { *; }</option>
                        <option>-keep class com.ts.sunshine.controller.** { *; }</option>
                        <!-- This option will save all original class files (without obfuscate) in service package-->
                        <!--<option>-keep class com.slm.proguard.example.spring.boot.service { *; }</option>-->
                        <!-- This option will save all original interfaces files (without obfuscate) in all packages.-->
                        <option>-keep interface * extends * { *; }</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>
                        <!-- <lib>${java.home}/lib/spring-boot-starter-web-1.4.1.RELEASE.jar</lib> -->
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>6.0.3</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- <phase>none</phase> -->
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.ts.Application</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

 注意:

-keep class 类/包.**  表示保留类名

-keepclassmembers class 类/包.**{ *;} 表示保留类下边的所有变量,均不混淆