上文已实现打出可执行的jar包,在原来pom基础上,加入的配置即可。
<plugin> <groupId>com.github.wvengen</groupId> <artifactId>proguard-maven-plugin</artifactId> <version>2.0.13</version> <executions> <execution> <phase>package</phase> <goals> <goal>proguard</goal> </goals> </execution> </executions> <configuration> <attach>true</attach> <attachArtifactClassifier>pg</attachArtifactClassifier> <!-- attach 的作用是在 install 与 deploy 时将生成的 pg 文件也安装与部署 --> <options> <!-- 详细配置方式参考 ProGuard 官方文档 --> <!--<option>-dontobfuscate</option>--> <option>-ignorewarnings</option> <!--忽略所有告警--> <option>-dontshrink</option> <!--不做 shrink --> <option>-dontoptimize</option> <!--不做 optimize --> <option>-dontskipnonpubliclibraryclasses</option> <option>-dontskipnonpubliclibraryclassmembers</option> <option>-repackageclasses com.lwf.proguard.example.project2.pg</option> <!--平行包结构(重构包层次),所有混淆的类放在 pg 包下--> <!-- 以下为 Keep,哪些内容保持不变,因为有一些内容混淆后(a,b,c)导致反射或按类名字符串相关的操作失效 --> <option>-keep class **.package-info</option> <option>-keep class **.TestClass { *;}</option> <!--保持包注解类--> <option>-keepattributes Signature</option> <!--JAXB NEED,具体原因不明,不加会导致 JAXB 出异常,如果不使用 JAXB 根据需要修改--> <!-- Jaxb requires generics to be available to perform xml parsing and without this option ProGuard was not retaining that information after obfuscation. That was causing the exception above. --> <option>-keepattributes SourceFile,LineNumberTable,*Annotation*</option> <!--保持源码名与行号(异常时有明确的栈信息),注解(默认会过滤掉所有注解,会影响框架的注解)--> <option>-keepclassmembers enum com.lwf.proguard.example.project2.** { *;}</option> <!--保持枚举中的名子,确保枚举 valueOf 可以使用--> <option>-keep class com.lwf.proguard.example.project2.bean.** { *;}</option> <!--保持 Bean 类,(由于很多框架会对 Bean 中的内容做反射处理,请根据自己的业务调整) --> <option>-keep class com.lwf.proguard.example.project2.Project2 { public void init(); public void destroy(); } </option> <!-- 保持对外的接口性质类对外的类名与方法名不变 --> </options> <outjar>${project.build.finalName}-pg</outjar> <libs> <lib>${java.home}/lib/rt.jar</lib> </libs> </configuration> </plugin>
pom文件全文如下:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lwf.MavenPackage</groupId> <artifactId>MavenPackage</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MavenPackage</name> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>fbcds</groupId> <artifactId>fbcds</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>ojdbc7</groupId> <artifactId>ojdbc7</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>.\lib</extdirs> </compilerArguments> </configuration> </plugin> <!-- 方法一:使用 maven-shade-plugin插件打可执行包--> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.lwf.test.TestClass</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> --> <!-- 方法二:使用 maven-Assembly-plugin插件打可执行包--> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.lwf.test.TestClass</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> --> <!-- 方法三:使用 onejar-maven-plugin插件打可执行包--> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.lwf.test.TestClass</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>com.jolira</groupId> <artifactId>onejar-maven-plugin</artifactId> <version>1.4.4</version> <executions> <execution> <configuration> <attachToBuild>true</attachToBuild> <classifier>onejar</classifier> </configuration> <goals> <goal>one-jar</goal> </goals> </execution> </executions> </plugin> --> <!-- 方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>**/log4j.properties</exclude> </excludes> <archive> <!-- 包里面是否包含pom.xml文件和pom.properties文件 --> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <addClasspath>true</addClasspath> <mainClass>com.lwf.test.TestClass</mainClass> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.5.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- exclude junit, we need runtime dependency only --> <includeScope>runtime</includeScope> <outputDirectory>${project.build.directory}/lib/</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.github.wvengen</groupId> <artifactId>proguard-maven-plugin</artifactId> <version>2.0.13</version> <executions> <execution> <phase>package</phase> <goals> <goal>proguard</goal> </goals> </execution> </executions> <configuration> <attach>true</attach> <attachArtifactClassifier>pg</attachArtifactClassifier> <!-- attach 的作用是在 install 与 deploy 时将生成的 pg 文件也安装与部署 --> <options> <!-- 详细配置方式参考 ProGuard 官方文档 --> <!--<option>-dontobfuscate</option>--> <option>-ignorewarnings</option> <!--忽略所有告警--> <option>-dontshrink</option> <!--不做 shrink --> <option>-dontoptimize</option> <!--不做 optimize --> <option>-dontskipnonpubliclibraryclasses</option> <option>-dontskipnonpubliclibraryclassmembers</option> <option>-repackageclasses com.lwf.proguard.example.project2.pg</option> <!--平行包结构(重构包层次),所有混淆的类放在 pg 包下--> <!-- 以下为 Keep,哪些内容保持不变,因为有一些内容混淆后(a,b,c)导致反射或按类名字符串相关的操作失效 --> <option>-keep class **.package-info</option> <option>-keep class **.TestClass { *;}</option> <!--保持包注解类--> <option>-keepattributes Signature</option> <!--JAXB NEED,具体原因不明,不加会导致 JAXB 出异常,如果不使用 JAXB 根据需要修改--> <!-- Jaxb requires generics to be available to perform xml parsing and without this option ProGuard was not retaining that information after obfuscation. That was causing the exception above. --> <option>-keepattributes SourceFile,LineNumberTable,*Annotation*</option> <!--保持源码名与行号(异常时有明确的栈信息),注解(默认会过滤掉所有注解,会影响框架的注解)--> <option>-keepclassmembers enum com.lwf.proguard.example.project2.** { *;}</option> <!--保持枚举中的名子,确保枚举 valueOf 可以使用--> <option>-keep class com.lwf.proguard.example.project2.bean.** { *;}</option> <!--保持 Bean 类,(由于很多框架会对 Bean 中的内容做反射处理,请根据自己的业务调整) --> <option>-keep class com.lwf.proguard.example.project2.Project2 { public void init(); public void destroy(); } </option> <!-- 保持对外的接口性质类对外的类名与方法名不变 --> </options> <outjar>${project.build.finalName}-pg</outjar> <libs> <lib>${java.home}/lib/rt.jar</lib> </libs> </configuration> </plugin> </plugins> </build> </project>
具体见附件项目文件。