Bad_Pan
Bad_Pan

Reputation: 508

Proguard + Spring Boot + Maven maintain structure

I have the following problem using proguard-maven-plugin on a spring boot application (ver.1.5.8). I 'm trying to obfuscate the final jar file but some files are missing from the obfuscated jar. Basically I 'm missing the BOOT-INF and org.springframework.boot.loader packages and also META-INF has a different structure than the original jar file.

Here is what I have in the pom.xml

<profiles>
    <profile>
        <id>obfuscation</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.wvengen</groupId>
                    <artifactId>proguard-maven-plugin</artifactId>
                    <version>2.0.14</version>
                    <executions>
                        <execution>
                            <id>proguard</id>
                            <phase>package</phase>
                            <goals>
                                <goal>proguard</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <obfuscate>true</obfuscate>
                        <injar>${project.build.finalName}.jar</injar>
                    <outjar>${project.build.finalName}-min.jar</outjar>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <proguardInclude>${basedir}/proguard.conf</proguardInclude>
                        <libs>
                            <lib>${java.home}/lib/rt.jar</lib>
                            <lib>${java.home}/lib/jsse.jar</lib>
                        </libs>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>net.sf.proguard</groupId>
                            <artifactId>proguard-base</artifactId>
                            <version>5.3.3</version>
                            <scope>runtime</scope>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Here is my proguard.conf

-dontshrink
-verbose

-optimizationpasses 3 

-keepdirectories
-keep class * extends *
-keep interface * extends *

-keepclasseswithmembers public class * { 
    public static void main(java.lang.String[]); 
} 

-keepclassmembers class * {
    @org.springframework.beans.factory.annotation.Autowired *;
}

I execute the obfuscation using the following command

mvn clean package -DskipTests=true -P obfuscation

How can I mantain the original structure of the jar file in the obfuscated jar. I have tried out different ways but with no luck.

Thanks in advance

Upvotes: 2

Views: 4071

Answers (2)

Shailesh Chandra
Shailesh Chandra

Reputation: 2340

I figured out & solved your problem and finally it conclusion is same what was my first answer. As per spring docs

https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

This configuration will repackage a jar or war that is built during the package phase of the Maven lifecycle.

As a matter of fact spring plugin is picking your in-jar for the repackage.

To solve this problem, keep the name of in-jar and out-jar same, as I suggested in my maven snippet.

<configuration>
                <obfuscate>true</obfuscate>
                <injar>${project.build.finalName}.jar</injar>
                <outjar>${project.build.finalName}.jar</outjar>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <proguardInclude>${basedir}/proguard.conf</proguardInclude>
                <libs>
                     <lib>${java.home}/lib/rt.jar</lib>
                     <lib>${java.home}/lib/jsse.jar</lib>
                </libs>
            </configuration>

don't worry proguard will keep a copy of original in-jar like below

Output

Upvotes: 4

Shailesh Chandra
Shailesh Chandra

Reputation: 2340

It appears to me that your spring boot plug-in is not packaging properly the obfuscated jar

below configuration worked for me

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <start-class>org.springframework.boot.loader.JarLauncher</start-class>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Upvotes: 0

Related Questions