Developers, Developers, Developers! Maksim Sorokin IT Blog

23Aug/108

Obfuscating Several Jars in One Single Maven Build with ProGuard

ProGuard is nice and free tool for obfuscating your code. Actually, it can do a lot more: shrink, optimize and verify your application. But here we will stick solely on obfuscating several jars in one build with ProGuard and Maven.

There are some documentation on ProGuard Maven Plugin website, something on Stack Overflow and ofcourse in official documentation.

Say, you have a complex Java Web Start application consisting of several modules. And you want to obfuscate each module before release. Of course, you do not want to obfuscate for internal usage, but want to do it only before release. What you can do is to use Webstart Maven Plugin to assemble and sign the application. But before running it, you will obfuscate it with ProGuard Maven Plugin.

Now, the question is. How do you obfuscate several jars separately? The solution is pretty simple and based on old trick. You can have several different executions, but with different ids, each outputting obfuscate application to a different jar. For example:

...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>com.pyx4me</groupId>
        <artifactId>proguard-maven-plugin</artifactId>
        <version>2.0.4</version>
        <executions>
          <execution>
            <id>first</id>
            <phase>package</phase>
            <goals>
              <goal>proguard</goal>
            </goals>

            <configuration>
              <outjar>first.jar</outjar>
              <options>
                <option>-keep public class dk.sorokin.maksim.first.Hello1 { public static void main(java.lang.String[]); }</option>
              </options>
              <assembly>
                <inclusions>
                  <inclusion>
                    <groupId>dk.sorokin.maksim</groupId>
                    <artifactId>first</artifactId>
                  </inclusion>
                </inclusions>
              </assembly>
              <libs>
                <lib>${java.home}/lib/rt.jar</lib>
                <lib>${java.home}/lib/javaws.jar</lib>
              </libs>
            </configuration>
          </execution>

          <execution>
            <id>aaa</id>
            <phase>package</phase>
            <goals>
              <goal>proguard</goal>
            </goals>

            <configuration>
              <outjar>second.jar</outjar>
              <options>
                <option>-keep public class dk.sorokin.maksim.second.Hello2 { public String sayHello(); }</option>
              </options>
              <assembly>
                <inclusions>
                  <inclusion>
                    <groupId>dk.sorokin.maksim</groupId>
                    <artifactId>second</artifactId>
                  </inclusion>
                </inclusions>
              </assembly>
              <libs>
                <lib>${java.home}/lib/rt.jar</lib>
                <lib>${java.home}/lib/javaws.jar</lib>
              </libs>
            </configuration>
          </execution>
        </executions>
      </plugin>
....
Comments (8) Trackbacks (0)

Leave a comment


Trackbacks are disabled.