如果你不想让别人知道你太多的实现细节,那么可以通过proguard对自己的代码进行混淆。jdk版本支持1.7,不支持jdk1.8.
下面是pom.xml中响应的配置,您在http://mvnrepository.com/中是找不到net.sf.proguard4.8的版本的,告诉大家一个地址:proguard48jar,这个地址不经可以下载proguard-4.8.jar,还可以下载其他的文件。也是偶然的机会,我才发现了此地址。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.compile.version}</source>
<target>${java.compile.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<libs>
<lib>D:/Program Files/Java/jre7/lib/rt.jar</lib>
</libs>
<addMavenDescriptor>false</addMavenDescriptor>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>4.8</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
注意下方的目录结构,在工程的根目录添加proguard.conf文件,这个文件就是混淆的配置信息
下面是proguard.conf的内容信息:
#忽略警告信息
ignorewarnings
#不做 shrink
-dontshrink
#指定不去忽略包可见的库类的成员
-dontskipnonpubliclibraryclassmembers
# 通过指定数量的优化能执行
-optimizationpasses 3
# 指定不去忽略非公共的库类
-dontskipnonpubliclibraryclasses
# 输出生成信息
-verbose
#混淆时应用侵入式重载
-overloadaggressively
#优化时允许访问并修改有修饰符的类和类的成员
-allowaccessmodification
#确定统一的混淆类的成员名称来增加混淆
-useuniqueclassmembernames
#这里添加你不需要混淆的类
# --------- 保护类中的所有方法名 ------------
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
执行mvn install命令,执行后的日志
[proguard] Number of removed local variables: 3
[proguard] Number of removed exception blocks: 0
[proguard] Number of optimized local variable frames: 0
[proguard] Obfuscating...
[proguard] Printing mapping to [F:\ccbwork\dzmsoft-framework-base\target\proguard_map.txt]...
[proguard] Preverifying...
[proguard] Writing output...
[proguard] Preparing output jar [F:\ccbwork\dzmsoft-framework-base\target\com.dzmsoft.framework.base-2.0.1.jar]
[proguard] Copying resources from program jar [F:\ccbwork\dzmsoft-framework-base\target\com.dzmsoft.framework.base-2.0.1_proguard_base.jar] (filtered)
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ com.dzmsoft.framework.base ---
[INFO] Installing F:\ccbwork\dzmsoft-framework-base\target\com.dzmsoft.framework.base-2.0.1.jar to D:\Program Files (x86)\apache-maven-3.1.1\repository\com\dzmsoft\com.dzmsoft.framework.base\2.0.1\com.dzmsoft.framework.base-2.0.1.jar
[INFO] Installing F:\ccbwork\dzmsoft-framework-base\pom.xml to D:\Program Files (x86)\apache-maven-3.1.1\repository\com\dzmsoft\com.dzmsoft.framework.base\2.0.1\com.dzmsoft.framework.base-2.0.1.pom
[INFO] Installing F:\ccbwork\dzmsoft-framework-base\target\com.dzmsoft.framework.base-2.0.1-sources.jar to D:\Program Files (x86)\apache-maven-3.1.1\repository\com\dzmsoft\com.dzmsoft.framework.base\2.0.1\com.dzmsoft.framework.base-2.0.1-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
通过jd-gui工具查看,混淆后的结果如下图所示,是不是很爽,不过这么混淆工具类,自己估计也会受不了,下一步就需要调优配置,既达到隐藏细节的作用,又还能方便使用。