1、pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.tj.hoo</groupId><artifactId>pro-guard-demo</artifactId><version>0.0.1</version><name>ProGuard-Demo</name><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><source>${java.version}</source><target>${java.version}</target><compilerVersion>${java.version}</compilerVersion><compilerArgs>--enable-preview</compilerArgs></configuration></plugin><plugin><groupId>com.github.wvengen</groupId><artifactId>proguard-maven-plugin</artifactId><version>2.6.0</version><executions><!--以下配置说明执行mvn的package命令时候,会执行proguard--><execution><phase>package</phase><goals><goal>proguard</goal></goals></execution></executions><configuration><!--输入jar--><injar>${project.build.finalName}.jar</injar><!--输出jar--><outjar>${project.build.finalName}.jar</outjar><!--是否混淆--><obfuscate>true</obfuscate><!--配置文件--><proguardInclude>${project.basedir}/proguard.cfg</proguardInclude><!--额外的依赖--><!--<libs><lib></lib></libs>--><!--对输入jar继续过滤--><inLibsFilter>!META-INF/**</inLibsFilter><!--输出路径配置--><outputDirectory>${project.basedir}/target</outputDirectory><!--混淆的一些细节选项--><!--<options><option></option></options>--></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>
2、ProGuard配置
# 指定Java的版本
-target 17
# proguard会对代码进行优化压缩,他会删除从未使用的类或者类成员变量等
-dontshrink
# 是否关闭字节码级别的优化,如果不开启则设置如下配置
-dontoptimize
# 混淆时不生成大小写混合的类名,默认是可以大小写混合
-dontusemixedcaseclassnames
# 对于类成员的命名的混淆采取唯一策略
-useuniqueclassmembernames
# 混淆时不生成大小写混合的类名,默认是可以大小写混合
-dontusemixedcaseclassnames
# 混淆类名之后,对使用Class.forName('className')之类的地方进行相应替代
-adaptclassstrings# 对异常、注解信息予以保留
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
# 此选项将保存接口中的所有原始名称(不混淆)-->
-keepnames interface ** { *; }
# 此选项将保存所有软件包中的所有原始接口文件(不进行混淆)
#-keep interface * extends * { *; }
# 保留参数名,因为控制器,或者Mybatis等接口的参数如果混淆会导致无法接受参数,xml文件找不到参数
-keepparameternames
# 保留枚举成员及方法
-keepclassmembers enum * { *; }
# 不混淆所有类,保存原始定义的注释-
-keepclassmembers class * {@org.springframework.context.annotation.Bean *;@org.springframework.beans.factory.annotation.Autowired *;@org.springframework.beans.factory.annotation.Value *;@org.springframework.stereotype.Service *;@org.springframework.stereotype.Component *;}# 忽略warn消息
-ignorewarnings
# 指定不打印有关配置中潜在错误或遗漏的注释,例如类名中的拼写错误或缺少可能有用的选项
-dontnote com.tj.hoo.proguard.demo.ProGuardDemoApplication
# 打印配置信息
-printconfiguration
-keep public class com.tj.hoo.proguard.demo.ProGuardDemoApplication {public static void main(java.lang.String[]);}
3、测试代码
package com.tj.hoo.proguard.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ProGuardDemoApplication {public static void main(String[] args) {SpringApplication.run(ProGuardDemoApplication.class, args);}}
package com.tj.hoo.proguard.demo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Collections;
import java.util.Map;@RestController
@RequestMapping("/index")
public class IndexController {@GetMapping("/test01")public Map<String, Object> test01() {return Collections.singletonMap("name", "Jack");}}
4、命令
不支持JDK20,否则报错:[ERROR] Failed to execute goal com.github.wvengen:proguard-maven-plugin:2.6.0:proguard (default) on project pro-guard-demo: Obfuscation failed (result=1) -> [Help 1]
mvn clean package -Dmaven.test.skip=true