19

I'm trying to get Maven working with ProGuard.

What I want to achieve is the following:

  • Run ProGuard over my source files and produce obfuscated classes

  • Create a manifest file that references the main class so that I can execute it as a jar

  • Unpack all of the associated library jars and create one huge jar containing them all. This file should only contact .class and .xml files only.

  • Assemble them into .zip and tar.gz files that include various README.txt files and so on.

So far I've got something like this:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.class.path.MainClass</mainClass>
                    </manifest>
                </archive>
                <includes>
                    <include>**/*.class</include>
                    <include>**/*.xml</include>
                </includes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.pyx4me</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <configuration>
                <options>
                    <option>-allowaccessmodification</option>
                </options>
                <obfuscate>true</obfuscate>
                <injar>classes</injar>
                <outjar>${project.build.finalName}.jar</outjar>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <proguardInclude>src/main/assembly/proguard.conf</proguardInclude>
                <libs>
                    lib/rt.jar</lib>
                </libs>
            </configuration>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>
                                src/main/assembly/bin.xml
                            </descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

But I'm having no joy. Can anyone give me any vague pointers on this?

Thanks in advance, Matt

starblue
  • 55,348
  • 14
  • 97
  • 151
Sway
  • 1,647
  • 3
  • 16
  • 19
  • @starblue Maven doesn't have any `[assemblies]` concept. However, `[assembly]` is a very valid one. So please stop behaving like tags are for unique domains, excluding everything that does not match your vision of their use. Tags are for set, add more constraints. Don't forbid me to use a tag that applies to Maven too. This is freaking annoying. – Pascal Thivent Aug 02 '10 at 14:28
  • http://maven.apache.org/plugins/maven-assembly-plugin/ : This plugin generates "assemblies". – starblue Aug 07 '10 at 15:15

1 Answers1

11

Here is the configuration that had worked for me

<plugin>
    <groupId>com.pyx4me</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>true</obfuscate>
        <options>
            <option>-allowaccessmodification</option>
            <option>-keep public class com.class.path.MainClass { public *; public static *; }</option>
        </options>
        <injar>${project.build.finalName}.jar</injar>
        <outjar>${project.build.finalName}-small.jar</outjar>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
        </libs>
        <addMavenDescriptor>false</addMavenDescriptor>
    </configuration>
</plugin>

The final jar is the finalName-small.jar

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
  • Is there a reason you are using the package phase and not the process-classes phase? Process-classes runs after compile but before package so you can access target/classes before they are get jarred. – sal May 22 '09 at 19:14
  • It's been a while, but as far as I remember I found out that the plugin basically runs proguard, so it likes to get one jar and emit anther one. In other words - many trial and error, this one works... – David Rabinowitz May 22 '09 at 21:54
  • how do you deal with multiple Maven modules in the same project? do you run the plugin in the root pom.xml, do you run it in web ui module assembly, etc? – Alex May 23 '12 at 21:05
  • Sorry, never dealt with multiple artifacts in the same project. – David Rabinowitz Feb 15 '13 at 16:07