栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ProGuard不会混淆依赖关系的JAR

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ProGuard不会混淆依赖关系的JAR

proguard-maven-plugin
会混淆你的项目,而不是次要神器的主伪影
jar-with-dependencies
maven-assembly-plugin
产生。

您需要

jar-with-dependencies
通过指定
injar
属性来配置插件以混淆:

指定要处理的应用程序的输入jar名称(或wars,ears,zips)。

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

现在,在您的POM中,插件的执行顺序也存在问题:我们需要确保在

maven-assembly-plugin
之前执行
proguard-maven-plugin
。因此,最好为
maven-assembly-plugin
绑定到该
package
阶段定义一个显式执行,而不是使用从命令行手动调用它
assembly:single
。这将是配置:

<plugin>    <artifactId>maven-assembly-plugin</artifactId>    <executions>        <execution> <id>assembly</id> <goals>     <goal>single</goal> </goals> <phase>package</phase> <configuration>     <descriptorRefs>         <descriptorRef>jar-with-dependencies</descriptorRef>     </descriptorRefs> </configuration>        </execution>    </executions></plugin>

然后,您只需要确保

proguard-maven-plugin
插件配置在POM中就可以了。

完成此操作后,使用调用Maven

mvn clean install
将导致带有依赖性的混淆jar。


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

  • https://repo.spongepowered.org/maven
    解决
    spongeapi
    依赖性。
  • http://maven.restlet.com
    解决
    org.restlet.jse
    依赖性。

对于这2个依赖项,ProGuard会生成警告,因为该

org.restlet.ext.jackson
依赖项使用
com.sun.msv.*
不在构建路径上的类。由于我认为您的代码当前正在工作,这意味着不需要包含这些类,并且可以忽略这些警告。因此,我添加了该
-dontwarn
选项,以便在出现警告时ProGuard不会出错。

我能够针对其成功混淆依赖关系的jar的最终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.mycompany</groupId>    <artifactId>myproduct</artifactId>    <version>1.0-SNAPSHOT</version>    <repositories>       <repository><id>spongepowered</id><url>https://repo.spongepowered.org/maven</url>       </repository>       <repository><id>restlet</id><url>http://maven.restlet.com</url>       </repository>    </repositories>    <properties>        <restlet-version>2.3.5</restlet-version>    </properties>    <build>       <resources> <resource>     <directory>${project.basedir}/src/main/resources</directory>     <filtering>true</filtering> </resource>        </resources>        <plugins> <plugin>     <artifactId>maven-compiler-plugin</artifactId>     <version>3.3</version>     <configuration>         <source>1.8</source>         <target>1.8</target>     </configuration> </plugin> <plugin>     <groupId>org.prehaus.mojo</groupId>     <artifactId>templating-maven-plugin</artifactId>     <version>1.0-alpha-3</version>     <executions>         <execution>  <id>filter-src</id>  <goals>      <goal>filter-sources</goal>  </goals>         </execution>     </executions> </plugin> <plugin>     <artifactId>maven-assembly-plugin</artifactId>     <executions>         <execution>  <id>assembly</id>  <goals>      <goal>single</goal>  </goals>  <phase>package</phase>  <configuration>      <descriptorRefs>          <descriptorRef>jar-with-dependencies</descriptorRef>      </descriptorRefs>  </configuration>         </execution>     </executions> </plugin> <plugin>     <groupId>com.github.wvengen</groupId>     <artifactId>proguard-maven-plugin</artifactId>     <version>2.0.8</version>     <executions>         <execution>  <phase>package</phase>  <goals><goal>proguard</goal></goals>  <configuration>      <injar>${project.build.finalName}-jar-with-dependencies.jar</injar> <!-- make sure to obfuscate the jar with dependencies -->      <proguardVersion>5.2</proguardVersion>      <options>          <option>-allowaccessmodification</option>          <option>-dontoptimize</option>          <option>-dontshrink</option>          <option>-dontnote</option>          <option>-dontwarn</option> <!-- added option to ignore com.sun missing classes -->          <option>-keepattributes Signature</option>          <option>-keep class com.mycompany.MyPlugin { *; }</option>      </options>      <libs>          <lib>${java.home}/lib/rt.jar</lib>      </libs>      <dependencies>          <dependency>   <groupId>net.sf.proguard</groupId>   <artifactId>proguard-base</artifactId>   <version>5.2</version>   <scope>runtime</scope>          </dependency>      </dependencies>  </configuration>         </execution>     </executions> </plugin>        </plugins>    </build>    <dependencies>       <dependency> <groupId>org.spongepowered</groupId> <artifactId>spongeapi</artifactId> <version>3.0.0</version> <scope>provided</scope>        </dependency>        <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version>        </dependency>        <dependency> <groupId>org.easytesting</groupId> <artifactId>fest-assert-core</artifactId> <version>2.0M8</version> <scope>test</scope>        </dependency>        <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.10.19</version> <scope>test</scope>        </dependency>        <dependency> <groupId>org.restlet.jse</groupId> <artifactId>org.restlet</artifactId> <version>${restlet-version}</version>        </dependency>        <dependency> <groupId>org.restlet.jse</groupId> <artifactId>org.restlet.ext.jackson</artifactId> <version>${restlet-version}</version>        </dependency>        <dependency> <groupId>com.googlepre.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version>        </dependency>    </dependencies></project>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号