proguard-maven-plugin

Overview

The proguard-maven-plugin is maven2 plugin to execute ProGuard

Parameters

Regular proguard:proguard parameters

  • libs JVM libraries usually not in dependency list
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
        </libs>
    
  • You can use inFilter to apply ProGuard classpathentry Filters to input jar
        <inFilter>!org/microemu/app/classloader/**</inFilter>
    
  • You can use inLibsFilter to apply ProGuard classpathentry Filters to all input lib jars
        <inLibsFilter>!META-INF/versions/**</inLibsFilter>
    
  • You can use outFilter to apply ProGuard classpathentry Filters to output jar
        <outFilter>!**ECLIPSEF.*</outFilter>
    
  • exclusions allow fine grain filter for dependency if includeDependency is enabled
        <exclusions>
            <exclusion>
                <groupId>org.grp</groupId><artifactId>art1</artifactId>
            </exclusion>
            <exclusion>
                <!-- org.grp:art2 without classifier will still be included -->
                <groupId>org.grp</groupId><artifactId>art2</artifactId><classifier>app<classifier>
            </exclusion>
        </exclusions>
    
  • assembly allows to bundle project dependency to resulting jar with different options e.g. -injars ProGuard args. Some flagged as library doesn`t need to be processed by ProGuard are added as -libraryjars and added to resulting jar by plugin after ProGuard is finished. For clarity param includeDependency will just add -libraryjars arg to ProGuard and Dependency is not included in resulting jar.

    You can use assembly to apply ProGuard classpathentry Filters to dependency jars

        <assembly>
            <inclusions>
                <inclusion>
                    <!-- add jar already prepossessed with different options -->
                    <groupId>org.microemu</groupId><artifactId>microemu-cldc</artifactId><classifier>4applet</classifier><library>true</library>
                    <!-- filter will not apply -->
                </inclusion>
                <inclusion>
                    <!-- filter out some classes see ProGuard classpathentry Filters -->
                    <groupId>org.microemu</groupId><artifactId>microemu-javase</artifactId>
                    <filter>!org/microemu/app/classloader/**</filter>
                </inclusion>
                <inclusion>
                    <groupId>org.microemu</groupId><artifactId>microemu-javase-swing</artifactId>
                </inclusion>
                <!-- groupId and artifactId support wildcards, including partial matching -->
                <!-- include all org.thirdparty artifacts -->
                <inclusion>
                    <groupId>org.thirdparty</groupId><artifactId>*</artifactId>
                </inclusion>
            </inclusions>
        </assembly>
        <exclusions>
            <!-- groupId and artifactId support wildcards, including partial matching -->
            <!-- exclude all org.thirdparty1 artifacts -->
            <exclusion>
                <groupId>org.thirdparty1</groupId><artifactId>*</artifactId>
            </exclusion>
            <!-- exclude all org.thirdparty2 artifacts beginning with 'util.' -->
            <exclusion>
                <groupId>org.thirdparty2</groupId><artifactId>util.*</artifactId>
            </exclusion>
            <!-- exclude all artifacts ending with '-macos' -->
            <exclusion>
                <groupId>*</groupId><artifactId>*-macos</artifactId>
            </exclusion>
        </exclusions>
    
  • ProGuard options

    Some ProGuard options are created automatically

    • -dontobfuscate When obfuscate is false
    • -libraryjars Are created based on libs and your project dependencies when includeDependency is true (by default).
    • -verbose When executing maven -X
    • -printmapping is based on the mappingFileName set in the configuration, defaults to '$project.build.directory/<mappingFileName>', while -printseeds is based on seedFileName and defaults to '$project.build.directory/<seedFileName>'

    Additional ProGuard configuration can be added using options or proguardInclude

  • Command line length workaround

    If you have a huge list of dependencies the list of -libraryjars the resulting command line to execute ProGaurd could become too long. On Windows the error message could look like CreateProcess error=206, The filename or extension is too long.

    • <generateTemporaryConfigurationFile>true</generateTemporaryConfigurationFile> makes the plugin pass the configuration by a temporary file instead of over the command line. Build performance should not be impacted by this.

Usage

Replace project artifact with obfuscated one. Original jar would be preserved in file project.build.finalName.jar_proguard_base.jar

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <executions>
                   <execution>
                       <phase>package</phase>
                       <goals><goal>proguard</goal></goals>
                   </execution>
                </executions>
                <configuration>
                    <options>
                        <option>-allowaccessmodification</option>
                        <option>-keep public class * extends java.applet.Applet { *; }</option>
                    </options>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
                </configuration>
            </plugin>
        </plugins>
    </build>

Create new file $project.build.finalName-small.jar

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <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>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <proguardInclude>${basedir}/proguard.conf</proguardInclude>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                        <lib>${java.home}/lib/jsse.jar</lib>
                    </libs>
                </configuration>
            </plugin>
        </plugins>
    </build>

Complex Applet creation example. See full MicroEmulator project sources for more details

    <dependencies>

        <dependency>
            <groupId>org.microemu</groupId>
            <artifactId>microemu-midp</artifactId>
            <version>${version}</version>
            <classifier>4applet</classifier>
        </dependency>

        <dependency>
            <groupId>org.microemu</groupId>
            <artifactId>microemu-javase-swing</artifactId>
            <version>${version}</version>
        </dependency>

        <dependency>
            <groupId>sun</groupId>
            <artifactId>applet-jsobject</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <executions>
                   <execution>
                       <phase>package</phase>
                       <goals><goal>proguard</goal></goals>
                   </execution>
                </executions>
                <configuration>
                    <proguardInclude>${basedir}/proguard.conf</proguardInclude>
                    <assembly>
                        <inclusions>
                            <inclusion>
                                <groupId>org.microemu</groupId><artifactId>microemu-cldc</artifactId><classifier>4applet</classifier><library>true</library>
                            </inclusion>
                            <inclusion>
                                <groupId>org.microemu</groupId><artifactId>microemu-midp</artifactId><classifier>4applet</classifier><library>true</library>
                            </inclusion>
                            <inclusion>
                                <groupId>org.microemu</groupId><artifactId>microemu-javase</artifactId>
                                <filter>!org/microemu/app/classloader/**</filter>
                            </inclusion>
                            <inclusion>
                                <groupId>org.microemu</groupId><artifactId>microemu-javase-swing</artifactId>
                            </inclusion>
                        </inclusions>
                    </assembly>
                    <exclusions>
                        <exclusion>
                            <groupId>org.microemu</groupId><artifactId>microemu-midp</artifactId>
                        </exclusion>
                    </exclusions>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                        <lib>${java.home}/lib/jsse.jar</lib>
                    </libs>
                </configuration>
            </plugin>

        </plugins>
    </build>

Use different version of ProGuard

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.wvengen</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <executions>
                   <execution>
                       <phase>package</phase>
                       <goals><goal>proguard</goal></goals>
                   </execution>
                </executions>
                <configuration>
                    <proguardVersion>4.5</proguardVersion>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>4.5</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>