java – ProGuard不会模糊JAR的依赖关系

前端之家收集整理的这篇文章主要介绍了java – ProGuard不会模糊JAR的依赖关系前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个项目与下面给出的pom.xml文件.当我发出命令mvn clean compile assembly:single install我想要Maven生成一个JAR,其中包含

>所有的依赖和
>我的代码的模糊版本.

它不起作用 – 我的代码在“jar-with-dependencies”文件中没有被模糊化.

当我运行mvn clean编译安装生成文件包含我的应用程序的模糊代码,但没有依赖关系.

为了拥有一个具有所有依赖关系和我的混淆代码的JAR文件,我该怎么办?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com.mycompany</groupId>
  8. <artifactId>myproduct</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <properties>
  12. <restlet-version>2.3.5</restlet-version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.spongepowered</groupId>
  17. <artifactId>spongeapi</artifactId>
  18. <version>3.0.0</version>
  19. <scope>provided</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>4.12</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.easytesting</groupId>
  28. <artifactId>fest-assert-core</artifactId>
  29. <version>2.0M8</version>
  30. <scope>test</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.mockito</groupId>
  34. <artifactId>mockito-core</artifactId>
  35. <version>1.10.19</version>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.restlet.jse</groupId>
  40. <artifactId>org.restlet</artifactId>
  41. <version>${restlet-version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.restlet.jse</groupId>
  45. <artifactId>org.restlet.ext.jackson</artifactId>
  46. <version>${restlet-version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>com.googlecode.json-simple</groupId>
  50. <artifactId>json-simple</artifactId>
  51. <version>1.1.1</version>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <resources>
  56. <resource>
  57. <directory>${project.basedir}/src/main/resources</directory>
  58. <filtering>true</filtering>
  59. </resource>
  60. </resources>
  61. <plugins>
  62. <plugin>
  63. <groupId>org.apache.maven.plugins</groupId>
  64. <artifactId>maven-compiler-plugin</artifactId>
  65. <version>3.3</version>
  66. <configuration>
  67. <source>1.8</source>
  68. <target>1.8</target>
  69. </configuration>
  70. </plugin>
  71. <plugin>
  72. <groupId>org.codehaus.mojo</groupId>
  73. <artifactId>templating-maven-plugin</artifactId>
  74. <version>1.0-alpha-3</version>
  75. <executions>
  76. <execution>
  77. <id>filter-src</id>
  78. <goals>
  79. <goal>filter-sources</goal>
  80. </goals>
  81. </execution>
  82. </executions>
  83. </plugin>
  84. <plugin>
  85. <artifactId>maven-assembly-plugin</artifactId>
  86. <configuration>
  87. <descriptorRefs>
  88. <descriptorRef>jar-with-dependencies</descriptorRef>
  89. </descriptorRefs>
  90. </configuration>
  91. </plugin>
  92. <plugin>
  93. <groupId>com.github.wvengen</groupId>
  94. <artifactId>proguard-maven-plugin</artifactId>
  95. <version>2.0.8</version>
  96. <executions>
  97. <execution>
  98. <phase>package</phase>
  99. <goals><goal>proguard</goal></goals>
  100. </execution>
  101. </executions>
  102. <configuration>
  103. <proguardVersion>5.2</proguardVersion>
  104. <options>
  105. <option>-allowaccessmodification</option>
  106. <option>-dontoptimize</option>
  107. <option>-dontshrink</option>
  108. <option>-dontnote</option>
  109. <option>-keepattributes Signature</option>
  110. <option>-keep class com.mycompany.MyPlugin { *; }</option>
  111. </options>
  112. <libs>
  113. <lib>${java.home}/lib/rt.jar</lib>
  114. </libs>
  115. <dependencies>
  116. <dependency>
  117. <groupId>net.sf.proguard</groupId>
  118. <artifactId>proguard-base</artifactId>
  119. <version>5.2</version>
  120. <scope>runtime</scope>
  121. </dependency>
  122. </dependencies>
  123. </configuration>
  124. </plugin>
  125. </plugins>
  126. </build>
  127. </project>

更新1(17.01.2016 19:54 MSK):更改了ProGuard配置,如下所示,但是mvn clean编译程序集:single仍然使用无模糊化的类文件生成JAR.

  1. <plugin>
  2. <groupId>com.github.wvengen</groupId>
  3. <artifactId>proguard-maven-plugin</artifactId>
  4. <version>2.0.8</version>
  5. <executions>
  6. <execution>
  7. <phase>package</phase>
  8. <goals><goal>proguard</goal></goals>
  9. </execution>
  10. </executions>
  11. <configuration>
  12. <proguardVersion>5.2</proguardVersion>
  13. <options>
  14. <option>-allowaccessmodification</option>
  15. <option>-dontoptimize</option>
  16. <option>-dontshrink</option>
  17. <option>-dontnote</option>
  18. <option>-keepattributes Signature</option>
  19. <option>-keep class com.mycompany.MyPlugin { *; }</option>
  20. </options>
  21. <injar>${project.build.finalName}-jar-with-dependencies.jar</injar>
  22. <libs>
  23. <lib>${java.home}/lib/rt.jar</lib>
  24. </libs>
  25. <dependencies>
  26. <dependency>
  27. <groupId>net.sf.proguard</groupId>
  28. <artifactId>proguard-base</artifactId>
  29. <version>5.2</version>
  30. <scope>runtime</scope>
  31. </dependency>
  32. </dependencies>
  33. </configuration>
  34. </plugin>

更新2(17.01.2016 20:29 MSK):mvn clean compile assembly:single install Failed.最后的消息可以看出here.

解决方法

proguard-maven-plugin将模糊项目的主要工件,而不是maven-assembly-plugin生成的辅助工件jar-with-dependencies.

您需要配置插件以通过指定injar属性来模糊jar-with-dependencies:

Specifies the input jar name (or wars,ears,zips) of the application to be processed.

  1. <injar>${project.build.finalName}-jar-with-dependencies.jar</injar>

现在,在POM中执行插件的顺序还有一个问题:我们需要确保在proguard-maven-plugin之前执行maven-assembly-plugin.因此,最好定义绑定到软件包阶段的maven-assembly-plugin的显式执行,而不是使用assembly:single从命令行手动调用它.这将是配置:

  1. <plugin>
  2. <artifactId>maven-assembly-plugin</artifactId>
  3. <executions>
  4. <execution>
  5. <id>assembly</id>
  6. <goals>
  7. <goal>single</goal>
  8. </goals>
  9. <phase>package</phase>
  10. <configuration>
  11. <descriptorRefs>
  12. <descriptorRef>jar-with-dependencies</descriptorRef>
  13. </descriptorRefs>
  14. </configuration>
  15. </execution>
  16. </executions>
  17. </plugin>

然后,您只需要确保proguard-maven-plugin插件配置在POM之后.

这样做后,使用mvn clean install调用Maven将导致一个混淆的jar包含依赖关系.

要测试您的实际POM,我添加了两个存储库:

> https://repo.spongepowered.org/maven来解决beachapi依赖关系.
> http://maven.restlet.com来解析org.restlet.jse依赖关系.

对于这2个依赖关系,ProGuard生成警告,因为org.restlet.ext.jackson依赖关系使用不在构建路径上的com.sun.msv.*类.由于我认为您的代码正在工作,所以这意味着这些类不需要被包括,并且这些警告可以被忽略.因此,我添加了-dontwarn选项,以便ProGuard在出现警告时不会发生错误.

我能够使用依赖关系成功模糊jar的最终POM如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.mycompany</groupId>
  5. <artifactId>myproduct</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <repositories>
  8. <repository>
  9. <id>spongepowered</id>
  10. <url>https://repo.spongepowered.org/maven</url>
  11. </repository>
  12. <repository>
  13. <id>restlet</id>
  14. <url>http://maven.restlet.com</url>
  15. </repository>
  16. </repositories>
  17. <properties>
  18. <restlet-version>2.3.5</restlet-version>
  19. </properties>
  20. <build>
  21. <resources>
  22. <resource>
  23. <directory>${project.basedir}/src/main/resources</directory>
  24. <filtering>true</filtering>
  25. </resource>
  26. </resources>
  27. <plugins>
  28. <plugin>
  29. <artifactId>maven-compiler-plugin</artifactId>
  30. <version>3.3</version>
  31. <configuration>
  32. <source>1.8</source>
  33. <target>1.8</target>
  34. </configuration>
  35. </plugin>
  36. <plugin>
  37. <groupId>org.codehaus.mojo</groupId>
  38. <artifactId>templating-maven-plugin</artifactId>
  39. <version>1.0-alpha-3</version>
  40. <executions>
  41. <execution>
  42. <id>filter-src</id>
  43. <goals>
  44. <goal>filter-sources</goal>
  45. </goals>
  46. </execution>
  47. </executions>
  48. </plugin>
  49. <plugin>
  50. <artifactId>maven-assembly-plugin</artifactId>
  51. <executions>
  52. <execution>
  53. <id>assembly</id>
  54. <goals>
  55. <goal>single</goal>
  56. </goals>
  57. <phase>package</phase>
  58. <configuration>
  59. <descriptorRefs>
  60. <descriptorRef>jar-with-dependencies</descriptorRef>
  61. </descriptorRefs>
  62. </configuration>
  63. </execution>
  64. </executions>
  65. </plugin>
  66. <plugin>
  67. <groupId>com.github.wvengen</groupId>
  68. <artifactId>proguard-maven-plugin</artifactId>
  69. <version>2.0.8</version>
  70. <executions>
  71. <execution>
  72. <phase>package</phase>
  73. <goals><goal>proguard</goal></goals>
  74. <configuration>
  75. <injar>${project.build.finalName}-jar-with-dependencies.jar</injar> <!-- make sure to obfuscate the jar with dependencies -->
  76. <proguardVersion>5.2</proguardVersion>
  77. <options>
  78. <option>-allowaccessmodification</option>
  79. <option>-dontoptimize</option>
  80. <option>-dontshrink</option>
  81. <option>-dontnote</option>
  82. <option>-dontwarn</option> <!-- added option to ignore com.sun missing classes -->
  83. <option>-keepattributes Signature</option>
  84. <option>-keep class com.mycompany.MyPlugin { *; }</option>
  85. </options>
  86. <libs>
  87. <lib>${java.home}/lib/rt.jar</lib>
  88. </libs>
  89. <dependencies>
  90. <dependency>
  91. <groupId>net.sf.proguard</groupId>
  92. <artifactId>proguard-base</artifactId>
  93. <version>5.2</version>
  94. <scope>runtime</scope>
  95. </dependency>
  96. </dependencies>
  97. </configuration>
  98. </execution>
  99. </executions>
  100. </plugin>
  101. </plugins>
  102. </build>
  103. <dependencies>
  104. <dependency>
  105. <groupId>org.spongepowered</groupId>
  106. <artifactId>spongeapi</artifactId>
  107. <version>3.0.0</version>
  108. <scope>provided</scope>
  109. </dependency>
  110. <dependency>
  111. <groupId>junit</groupId>
  112. <artifactId>junit</artifactId>
  113. <version>4.12</version>
  114. </dependency>
  115. <dependency>
  116. <groupId>org.easytesting</groupId>
  117. <artifactId>fest-assert-core</artifactId>
  118. <version>2.0M8</version>
  119. <scope>test</scope>
  120. </dependency>
  121. <dependency>
  122. <groupId>org.mockito</groupId>
  123. <artifactId>mockito-core</artifactId>
  124. <version>1.10.19</version>
  125. <scope>test</scope>
  126. </dependency>
  127. <dependency>
  128. <groupId>org.restlet.jse</groupId>
  129. <artifactId>org.restlet</artifactId>
  130. <version>${restlet-version}</version>
  131. </dependency>
  132. <dependency>
  133. <groupId>org.restlet.jse</groupId>
  134. <artifactId>org.restlet.ext.jackson</artifactId>
  135. <version>${restlet-version}</version>
  136. </dependency>
  137. <dependency>
  138. <groupId>com.googlecode.json-simple</groupId>
  139. <artifactId>json-simple</artifactId>
  140. <version>1.1.1</version>
  141. </dependency>
  142. </dependencies>
  143. </project>

猜你在找的Java相关文章