mvn proguard: Is there possible obfuscate dependency before copying - java

I have plugins configuration like below:
<project>
...
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>4.8</proguardVersion>
<obfuscate>true</obfuscate>
<includeDependency>true</includeDependency>
<injar>${project.artifactId}-${project.version}.jar</injar>
<outjar>${project.artifactId}-${project.version}-obf.jar</outjar>
<outputDirectory>${project.build.directory}</outputDirectory>
<maxMemory>256m</maxMemory>
<options>
<option>-keepclasseswithmembers class **, !some.specific.package.** { *; }</option>
<option>-keep public class some.specific.package.Main { public *; public static *; }</option>
<option>-allowaccessmodification</option>
<option>-dontskipnonpubliclibraryclasses</option>
<option>-dontskipnonpubliclibraryclassmembers</option>
<option>-dontwarn org.apache.log4j.**</option>
</options>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/javaws.jar</lib>
<lib>${javafx_home}/lib/jfxrt.jar</lib>
</libs>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>4.8</version>
</dependency>
</dependencies>
</plugin>
</build>
...
</project>
I want to obfuscate specific package some.specific.package for jar and their dependencies (before copying). Is there possible to do it?

did you try the command -keeppackagenames with negation (!) of the package you want to obfuscate? I tried teh following, with the result that just package org.neo4j.cineasts.domain was obfuscated. See below:
<configuration>
<proguardVersion>4.8</proguardVersion>
<obfuscate>true</obfuscate>
<includeDependency>true</includeDependency>
<injar>${project.artifactId}-${project.version}.jar</injar>
<outjar>${project.artifactId}-${project.version}-obf.jar</outjar>
<outputDirectory>${project.build.directory}</outputDirectory>
<maxMemory>256m</maxMemory>
<options>
<option>-keep public class org.neo4j.cineasts.controller.** { public *; public static *; }</option>
<option>-keeppackagenames !org.neo4j.cineasts.domain</option>
<option>-allowaccessmodification</option>
<option>-dontskipnonpubliclibraryclasses</option>
<option>-dontskipnonpubliclibraryclassmembers</option>
</options>
<libs>
<lib>C:/devtools/Java/jdk1.6.0_29_64/jre/lib/rt.jar</lib>
<lib>C:/devtools/Java/jdk1.6.0_29_64/jre/lib/javaws.jar</lib>
</libs>
</configuration>
Bests, Michael

Related

Use proguard to obfuscate one module in a multi-module project

I want to use proguard to obfuscate one module in a multi-module project and it doesn't run after obfuscation
The pom of the module needs to be obfuscated pom.xml :
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>${proguard.maven.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>${proguard.version}</proguardVersion>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}-hx.jar</outjar>
<obfuscate>true</obfuscate>
<options>
<option>-dontshrink</option>
<option>-dontoptimize</option>
<option>-useuniqueclassmembernames</option>
<option>-adaptclassstrings</option>
<option>-dontusemixedcaseclassnames</option>
<option>-keepattributes
Exceptions,
InnerClasses,
Signature,
Deprecated,
SourceFile,
LineNumberTable,
*Annotation*,
EnclosingMethod
</option>
<option>-keepnames interface **</option>
<option>-keepparameternames</option>
<option>-keep
class com.ctcc.core.SSCSApplication {
public static void main(java.lang.String[]);
}
</option>
<option>-ignorewarnings</option>
<option>-keep interface * extends * { *; }</option>
<option>-keep class com.xxx.core.config.xxxxx</option>
<option>-keep class com.xxx.core.config.yyyyy</option>
<option>-keep class com.xxx.core.config.zzzzz</option>
<option>-keep class com.xxx.core.config.ccccc</option>
<option>-keep class com.xxx.core.config.ddddd</option>
<option>-keep class com.xxx.core.filter.LoggingBodyFilter</option>
<option>-keep class com.xxx.core.mapper.** { *; }</option>
<option>-keep class com.fasterxml.jackson.** { *; }</option>
<option>-keep class org.json.JSONObject.** {** put(java.lang.String,java.util.Map);}</option>
<option>-keep class org.gitlab4j.**</option>
<option>-keepclassmembers class * {
#org.springframework.context.annotation.Bean *;
#org.springframework.beans.factory.annotation.Autowired *;
#org.springframework.beans.factory.annotation.Value *;
}
</option>
<option>-dontwarn com.fasterxml.jackson.databind.**</option>
<option>-dontwarn com.fasterxml.jackson.**</option>
</options>
<injarNotExistsSkip>true</injarNotExistsSkip>
<libs>
<lib>${java.home}/jmods/java.base.jmod</lib>
<lib>${java.home}/jmods/java.datatransfer.jmod</lib>
<lib>${java.home}/jmods/java.prefs.jmod</lib>
<lib>${java.home}/jmods/java.xml.jmod</lib>
<lib>${java.home}/jmods/java.desktop.jmod</lib>
<lib>${java.home}/jmods/</lib>
</libs>
</configuration>
<dependencies>
<dependency>
<groupId>com.guardsquare</groupId>
<artifactId>proguard-base</artifactId>
<version>${proguard.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xxx.core.SSCSApplication</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
After the package:
Internal structure in the obfuscated jar:
java -jar core-0.0.1-SNAPSHOT-hx.jar:
There is no main manifest attribute in core-0.0.1-SNAPSHOT-hx.jar

Parameterized Map not parameterized after proguard obfuscation

I am writing a plugin api for my application. The problem now is, that anything in my Maven Proguard config messed up the parameterized Maps, Lists and other parameterized objects.
So I have this code:
public final Map<String, PermissionGroup> getPermissionGroups() {
return Vars.PERMGROUPS;
}
Vars.PERMGROUPS is this:
public static final Map<String, PermissionGroup> PERMGROUPS = new HashMap<>();
So now, when I call getPermissionGroups I get a Map without parameters.
pom.xml
...
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<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}-final.jar</outjar>
<includeDependency>true</includeDependency>
<options>
<option>-keep public class my.package.Main { public static void main(java.lang.String[]); }</option>
<option>-keep public class my.package.plugins.** { *; }</option>
<option>-keep public class my.package.wrapper.** { *; }</option>
<option>-dontusemixedcaseclassnames</option>
<option>-dontskipnonpubliclibraryclasses</option>
<option>-verbose</option>
<option>-obfuscationdictionary dict.txt</option>
<option>-classobfuscationdictionary dict.txt</option>
<option>-packageobfuscationdictionary dict.txt</option>
</options>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
<assembly>
<inclusions>
...
</inclusions>
</assembly>
<archive>
<manifest>
<mainClass>my.package.Main</mainClass>
</manifest>
</archive>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>4.11</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
...
there are just 20000 random strings in dict.txt.
According to How do you stop Proguard from removing type parameters?. I just had to add -keepattributes Signature

How to assembly a project after using proguard-maven-plugin

I am trying to add an obfuscation step while packaging my app. I supposed that I had to insert the Proguard plugin between the compiler plugin and the assembly (the assembly just put all of my app and dependencies into one single jar).
<build>
<finalName>myApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<executions>
<execution>
<id>obfuscation-packaging</id>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
<configuration>
<proguardVersion>5.2</proguardVersion>
<obfuscate>true</obfuscate>
<attach>true</attach>
<appendClassifier>false</appendClassifier>
<addMavenDescriptor>true</addMavenDescriptor>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}.jar</outjar>
<injarNotExistsSkip>true</injarNotExistsSkip>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
</libs>
<options>
...
</options>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>myApp.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The compiling works fine, so does the obfuscation, but the assembly seems to be made with the normal jar, not the obfuscated one.
Here is my assembly.xml if needed:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>with-dep</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<!-- unpack les dépendances avant de les inclures dans le jar final de l'application -->
<unpack>true</unpack>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
In the end, myApp.jar is obfuscated, but myApp-with-dep.jar is not...
I also precise that I'm not quite sure about the configuration of my proguard plugin. If you see something weird, say it.
Thanks for your time.
I solved it by abandoning maven assembly plugin to shade plugin. Hope it might help someone.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!--Obfuscation-->
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<id>obfuscation-packaging</id>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
<configuration>
<proguardVersion>5.2</proguardVersion>
<obfuscate>true</obfuscate>
<addMavenDescriptor>true</addMavenDescriptor>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}.jar</outjar>
<mappingFileName>proguard_map.txt</mappingFileName>
<seedFileName>proguard_seed.txt</seedFileName>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
</libs>
<options>
...
</options>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>xxx.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

LifecycleExecutionException while using <proguardInclude></proguardInclude> tag in proguard-maven-plugin

I am using proguard-maven-plugin to obfuscate my jar. If I take plugin structure like this.
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<maxMemory>576m</maxMemory>
<proguardVersion>5.0</proguardVersion>
<obfuscate>true</obfuscate>
<injar>${project.build.finalName}.jar</injar>
<outjar>build/lib/${project.build.finalName}.jar</outjar>
<outputDirectory>${project.build.directory}</outputDirectory>
<libs>
<lib>${java.home}/jre/lib/rt.jar</lib>
<lib>${java.home}/jre/lib/jce.jar</lib>
</libs>
<options>
<option>-keep public class * { protected *;}</option>
</options>
</configuration>
</plugin>
for this it works fine and give me a obfuscated jar, but when try give it proguard.conf file in <proguardInclude></proguardInclude> in this tag instead of
<options>
<option></option>
</options> this tag it throw an exception
"**org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.github.wvengen:proguard-maven-plugin:2.0.11:proguard (default) on project**"
:like
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<maxMemory>576m</maxMemory>
<proguardVersion>5.0</proguardVersion>
<obfuscate>true</obfuscate>
<injar>${project.build.finalName}.jar</injar>
<outjar>build/lib/${project.build.finalName}.jar</outjar>
<outputDirectory>${project.build.directory}</outputDirectory>
<proguardInclude>${basedir}/proguard.conf</proguardInclude>
<libs>
<lib>${java.home}/jre/lib/rt.jar</lib>
<lib>${java.home}/jre/lib/jce.jar</lib>
</libs>
</configuration>
</plugin>
my proguard.conf contains -
keepattributes
Signature,LineNumberTable,*Annotation*,LocalVariableTable,SourceFile
-keep public class * { public protected *;}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepclassmembers class com/compny/blha/proj/Class/Container
{
}
-keep fields com/compny/blha/proj/Class/VersionUID long;
I got the solution, just I was need to change progaurd.conf like
-keepattributes
Signature,LineNumberTable,*Annotation*,LocalVariableTable,SourceFile
-keep public class * { public protected *;}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepclassmembers class com/compny/blha/proj/Class/Container
{
}
-keep fields com/compny/blha/proj/Class/VersionUID long;
I made mistake at "keepattributes" that should be "-keepattributes "

Maven, Proguard and assembly issues

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
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

Categories