How to generate an obfuscated jar file for a maven project

I am working on a java application using maven architecture project in Eclipse IDE and java 7

I need to generated an obfuscated a jar file which also must include all the dependencies obfuscated in it I've been checking some solutions around but I´ve been unable to get the desired result.

Some of the places that I've checked are this:

Lin1,Link2, Link3

from the two first links I based most of my current implementation

this is my pom.xml file:

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>

the error I'm getting is the following:

[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]

EDIT

Just to make things more clear When I build the project, two diferent jars are generated at the target folder (CacheClienteModelC-jar-with-dependecies.jar and CacheClienteModelC.jar), the file that I try to generate is CacheClienteModelC-small.jar (obfuscated jar) which is never created.

How should I configure the plugin to obfuscate my project's file?

Upvotes: 6

Views: 18281

Answers (1)

Uriel Arvizu
Uriel Arvizu

Reputation: 1916

You're using an older version of the proguard plugin for Maven, you should use this one, you can find it in the Maven Central repository.

Now as for your pom.xml, remove from the dependencies the com.pyx4me dependency, you won't need it there.

Now as for the plugin configuration try with something like this:

<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>

See how you specify the dependency for proguard in it? That should generate the obfuscated jar without problem. Try it out.

Upvotes: 3

Related Questions