Hiding dependencies by Shading and ProGuarding


Let’s say your library depends on commons-lang and you don’t want them exposed.

make it optional

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <scope>compile</scope>
  <optional>true</optional>
</dependency>

shade it

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.2</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <relocations>
          <relocation>
            <pattern>org.apache.commons.lang3</pattern>
            <shadedPattern>your.main.commons.lang3</shadedPattern>
          </relocation>
        </relocations>
      </configuration>
    </execution>
  </executions>
</plugin>

proguard it

<plugin>
  <groupId>com.github.wvengen</groupId>
  <artifactId>proguard-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>proguard</id>
      <phase>package</phase>
      <goals>
        <goal>proguard</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <addMavenDescriptor>true</addMavenDescriptor>
    <includeDependency>flase</includeDependency>
    <options>
      <option>
        -dontobfuscate
        -dontoptimize
        -keepattributes *Annotation*
        -keepattributes Signature
        -keepattributes InnerClasses
        -keep class your.main.* { *; }
      </option>
    </options>
    <libs>
      <lib>${java.bootstrap.classes}</lib>
      <lib>${java.cryptographic.extension.classes}</lib>
      <lib>${java.secure.socket.extension.classes}</lib>
    </libs>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>net.sf.proguard</groupId>
      <artifactId>proguard-base</artifactId>
      <version>5.2.1</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</plugin>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s