maven混淆插件只对方法体和常量混淆
时间: 2024-11-09 07:21:43 浏览: 11
java 代码混淆,网上可以找到的demo
在Maven项目中,要使用混淆插件(maven-shade-plugin或maven-proguard-plugin)只对方法体和常量进行混淆,你需要编写特定的ProGuard规则并配置混淆过程。这里是一个简单的例子:
首先,创建一个名为`proguard-rules.pro`的配置文件,并设置规则如下:
```properties
# 保持公共和私有访问修饰符的类和字段
-keepattributes AccessFlags
# 保持注解
-dontwarn com.yourcompany.annotation.*
# 保持原始包名,以便在运行时仍然能找到类和方法
-keep class com.yourcompany.package.** { *; }
-keep class * extends java.lang.Throwable { *; } # 保持异常类及其构造函数
# 混淆方法体和常量
-renamesourcefileattribute SourceFile
-renameclasses "your_new_package.$1" # 将类名替换为你希望的新名字
-optimizations !method/inlining/dryrun, !class/merging/except/system/classes, !code/simplification/arithmetic, !variable/usage:catch, !field/*, !class/merger
-forceprocessing
# 模糊化方法
-keep class com.yourcompany.package.YourClass {
public void yourMethod() @interface YourAnnotation; // 只混淆方法体
public final static *; // 混淆静态常量
private *; // 不混淆非公共字段
}
```
在`pom.xml`中,配置maven-shade-plugin或maven-proguard-plugin来使用这个规则:
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createSourcesJar>true</createSourcesJar>
<relocations>
<relocation>
<pattern>com.yourcompany.package</pattern>
<shadedPattern>your.new.package</shadedPattern>
</relocation>
</relocations>
<minimize>true</minimize>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your.main.ClassName</mainClass>
</transformer>
<!-- 使用ProGuardTransformer -->
<transformer implementation="com.github.jengzhihao.mvnproguard.MavenProguardTransformer">
<rulesFile>path/to/proguard-rules.pro</rulesFile>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
阅读全文