如何为maven项目生成混淆后的jar文件

How to generate an obfuscated jar file for a maven project

我正在使用 Eclipse IDE 和 java 7 java 中的 maven 架构项目开发 java 应用程序

我需要生成一个混淆的 jar 文件,其中还必须包含所有混淆的依赖项我一直在检查一些解决方案,但我一直无法获得所需的结果。

我查过的一些地方是这样的:

Lin1,Link2, Link3

来自我当前大部分实现的前两个链接

这是我的 pom.xml 文件:

pom.xml

<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>modeloconfigurador.cache</groupId>
    <artifactId>CacheClienteModelC</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>modeloconfigurador.cache</groupId>
            <artifactId>Serializer</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>

        <!--<dependency> -->
        <!--<groupId>com.pyx4me</groupId> -->
        <!--<artifactId>proguard-maven-plugin</artifactId> -->
        <!--<version>2.0.4</version> -->
        <!-- </dependency> -->
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>4.10</version>
        </dependency>


    </dependencies>


    <build>
        <finalName>CacheClienteModelC</finalName>
        <plugins>

            <!-- download source code in Eclipse, best practice -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>

                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <!-- Maven Assembly Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                        <manifest>
                            <mainClass>main.Main</mainClass>
                        </manifest>
                    </archive>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- bind to the packaging phase -->
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- comienza configuracion proguard -->


            <plugin>
                <groupId>com.pyx4me</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>4.10</proguardVersion>
                    <options>

                        <injar>${project.build.finalName}.jar</injar>
                        <outjar>${project.build.finalName}-small.jar</outjar>
                        <!-- <injar>CacheClienteModelC-jar-with-dependencies.jar</injar> -->
                        <!-- <outjar>CacheClienteModelC-small.jar</outjar> -->
                        <option>-allowaccessmodification</option>
                        <option>-keep public class main.Main { *; }</option>
                    </options>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                        <lib>${java.home}/lib/jce.jar</lib>
                    </libs>
                </configuration>
                <dependencies>

                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>4.10</version>
                        <!--<version>5.2.1</version> -->
                    </dependency>

                </dependencies>
            </plugin>


            <!-- termina configuracion proguard -->
        </plugins>


    </build>


</project>

我得到的错误如下:

[ERROR] Failed to execute goal com.pyx4me:proguard-maven-plugin:2.0.4:proguard (default) on project CacheClienteModelC: Can't rename E:\Users\B267481\Documents\ServidorDesarroCreditoAcertum\CacheClienteModelC\target\CacheClienteModelC.jar -> [Help 1]

编辑

只是为了让事情更清楚 当我构建项目时,在目标文件夹中生成了两个不同的 jar(CacheClienteModelC-jar-with-dependecies.jar 和 CacheClienteModelC.jar),我尝试生成的文件是 CacheClienteModelC-small.jar (混淆的 jar)从未创建过。

我应该如何配置插件来混淆我的项目文件?

您使用的是旧版本的 Maven proguard 插件,您应该使用 this 一个,您可以在 Maven 中央存储库中找到它。

现在至于你的 pom.xml,从依赖中删除 com.pyx4me 依赖,你在那里不需要它。

现在关于插件配置尝试这样的东西:

<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.10</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>true</obfuscate>
        <injar>${project.build.finalName}.jar</injar>
        <outjar>${project.build.finalName}-small.jar</outjar>
        <includeDependency>true</includeDependency>
        <options>
            <option>-keep public class your.package.Main { *; }</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jce.jar</lib>
        </libs>
        <archive>
            <manifest>
                <mainClass>Main</mainClass>
                <packageName>your.package</packageName>
            </manifest>
        </archive>

    </configuration>
    <dependencies>

        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>4.10</version>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
</plugin>

看看你是怎么在里面指定proguard的依赖的?那应该毫无问题地生成混淆的 jar。试试吧。