4

I am having a Spring boot multi-module maven project running with Java8. I need to obfuscate using proguard. My requirement is only to obfuscate my code and keep everything as-is. So my project looks like below:

enter image description here

Module B has a dependency on Module A. Module C has a dependency on Module A and B both. Module D also has a dependency on Module A and B both. Module E is not dependent on any other module.

Assembly outputs the jars of Module C, Module D, and Module E which runs as a separate spring boot application. All the dependencies of Module C and Module D go into the lib dir i.e. Module C has a lib folder which has all other dependencies as a jar along with a separate jar of Module A and Module B respectively.

So when we extract the Jar file of Module C, it looks like the below image:

enter image description here

So I need to obfuscate my code so I get started with the Module C, therefore I updated the pom.xml of Module C:

<proguard.version>6.2.2</proguard.version>

<proguard.maven.plugin.version>2.3.1</proguard.maven.plugin.version>

 <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>
                    <proguardInclude>proguard.conf</proguardInclude>
                    <putLibraryJarsInTempDir>true</putLibraryJarsInTempDir>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                    </libs>
                    <assembly>
                        <inclusions>
                            <inclusion>
                                <groupId>com.xyz</groupId>
                                <artifactId>module-a</artifactId>
                            </inclusion>
                            <inclusion>
                                <groupId>com.xyz</groupId>
                                <artifactId>module-b</artifactId>
                            </inclusion>
                        </inclusions>
                    </assembly>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard-base</artifactId>
                        <version>${proguard.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.3.RELEASE</version>
                <configuration>
                    <executable>true</executable>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

And here is my proguard.conf

-libraryjars <java.home>/lib/rt.jar
    
-target 1.8 ##Specify the java version number
-dontshrink ##Default is enabled, here the shrink is turned off, that is, the unused classes/members are not deleted.
-dontoptimize ##Default is enabled, here to turn off bytecode level optimization
-useuniqueclassmembernames ## Take a unique strategy for confusing the naming of class members
-adaptclassstrings ## After confusing the class name, replace it with a place like Class.forName('className')
-dontnote
-ignorewarnings ## warnings are ignored
-dontwarn
-keep public class * extends org.springframework.boot.web.support.SpringBootServletInitializer
-keepdirectories ## Keep the package structure
-keepclasseswithmembers public class * { public static void main(java.lang.String[]);} ##Maintain the class of the main method and its method name
-keepclassmembers enum * { *; }  ##Reserving enumeration members and methods
-keepclassmembers class * {
     @org.springframework.beans.factory.annotation.Autowired *;
     @org.springframework.beans.factory.annotation.Qualifier *;
     @org.springframework.beans.factory.annotation.Value *;
     @org.springframework.beans.factory.annotation.Required *;
     @org.springframework.context.annotation.Bean *;
     @org.springframework.context.annotation.Primary *;
     @org.springframework.boot.context.properties.ConfigurationProperties *;
     @org.springframework.boot.context.properties.EnableConfigurationProperties *;
     @javax.inject.Inject *;
     @javax.annotation.PostConstruct *;
     @javax.annotation.PreDestroy *;
}

-keep class java.xml.bind.** { *; }
-keep @org.springframework.boot.autoconfigure.SpringBootApplication class org.openskye.**
-keep @org.springframework.context.annotation.Configuration class org.openskye.**
-keep @org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration class * 

-keep class microsoft.exchange.webservices.** { *; }
-keep @org.springframework.cache.annotation.EnableCaching class *
-keep @org.springframework.context.annotation.Configuration class *
-keep @org.springframework.boot.context.properties.ConfigurationProperties class *
-keep @org.springframework.boot.autoconfigure.SpringBootApplication class *
-keep @org.springframework.boot.autoconfigure.batch class *
-allowaccessmodification
-keepattributes Signature
-keepattributes *Annotation*
-keepattributes EnclosingMethod
-keepattributes InnerClasses
-keepattributes Annotation
-keepdirectories com.jayk.springboot.proguard.obfuscationdemo
-keepdirectories org.springframework.boot.autoconfigure
## Do not change names of the getters and setter, if you remove this ##thymeleaf unable to find the getter and setter i.e: ##${greetingDTO.message}
-keepclassmembers class * {
    *** get*();
    void set*(***);
}
-keepclassmembernames class * {
     java.lang.Class class$(java.lang.String);
     java.lang.Class class$(java.lang.String, boolean);
}
-keepclassmembers enum * {
     public static **[] values();
     public static ** valueOf(java.lang.String);
     public static ** fromValue(java.lang.String);
}
 -keepattributes RuntimeVisibleAnnotations
    -keep @javax.persistence.* class * {
       *;
}
-keepnames class * implements java.io.Serializable
-keepclassmembernames public class com.test.blah.config.liquibase.AsyncSpringLiquibase
-keepclassmembers class * implements java.io.Serializable {
     static final long serialVersionUID;
     private static final java.io.ObjectStreamField[] serialPersistentFields;
     !static !transient <fields>;
     !private <fields>;
     !private <methods>;
     private void writeObject(java.io.ObjectOutputStream);
     private void readObject(java.io.ObjectInputStream);
     java.lang.Object writeReplace();
     java.lang.Object readResolve();
}
-keepclassmembers class * {
     @org.springframework.beans.factory.annotation.Autowired *;
     @org.springframework.security.access.prepost.PreAuthorize <methods>;
}

 -keepclassmembers class * {
    @org.springframework.beans.factory.annotation.Value *;
    @org.springframework.context.annotation.Bean *;
    @org.springframework.transaction.annotation.Transactional *;
    }
   

When I ran the mvn clean install command it ends up with three jars as usually when we extract the ModuleC jar, I found the structure has been changed:

enter image description here

And when I run the ModuleC jar it throwing the below error message:

Caused by: java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'Default': file:/C:/Users/admin/Downloads/ABC-proguard_3.52.0/ABC-proguard_3.52.0/assembly/target/xyz-3.52.0-SNAPSHOT-bin/module-c/3.52.0-SNAPSHOT/module-c.jar, file:/C:/Users/admin/Downloads/ABC-proguard_3.52.0/ABC-proguard_3.52.0/assembly/target/xyz-3.52.0-SNAPSHOT-bin/module-c/3.52.0-SNAPSHOT/module-c.jar

Please suggest, how can I obfuscate the code with minimum possible changes. Any suggestions will be appreciated. Thanks in advance.

Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88

0 Answers0