Archiv für den Monat: April 2015

ProGuard optimizer and obfuscator in multi-module maven projects

ProGuard is a tool that processes java .class files and generates smaller, faster and harder to read bytecode.
I had to use it for a single module in a multi-module maven project, and found no documentation for that, thus I describe the steps here.

In the module that should be obfuscated, a build step has to be configured in maven. The important thing here is the configuration with „<attach>true“, so that the obfuscated file is exported to the repository and hence can be used by other modules.

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.8</version>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>5.2</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>5.2</proguardVersion>
                    <attach>true</attach>
                    <obfuscate>true</obfuscate>
                    <options>
                        <option>-keep public class package.ParameterObject{ public *;}</option>
                        <option>-keep public class package.Implementation{public static void
                            process(package.ParameterObject);}
                        </option>
                        <option>-optimizationpasses 4</option>
                    </options>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
                </configuration>
            </plugin>
        </plugins>
    </build>

If you run the build, you can see that two artifacts are generated now by this module: A module-1.0.0-SNAPSHOT.jar that has the same content as before, and a module-1.0.0-SNAPSHOT-small.jar that contains the small and obfuscated code. You can pick another name instead of „small“ by specifying the „attachArtifactClassifier“-property in the configuration.

How do you use this obfuscated code from another module in the project? Pretty easy, the small-suffix (or however you named it) can be used as the classifier in the maven-dependency:

        <dependency>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <classifier>small</classifier>
        </dependency>

Run the build and look at the packaged artifact (for example a .war), it will no longer contain the clear bytecode, but only the obfuscated library.