Search code examples
spring-bootmavenproguard

Spring Boot returns 404 after generated jar file with proguard


I have a multi module maven project runs with Spring Boot and Webflux under Netty. Si I was using proguard maven plugin to generate obfuscated jar file. My problem is everything looks fine when I look at the logs but when I send a request to it I get 404 error.

The build section at pom.xml looks like:

<build>
    <plugins>
        <plugin>
            <groupId>com.github.wvengen</groupId>
            <artifactId>proguard-maven-plugin</artifactId>
            <version>2.5.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>proguard</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <proguardVersion>6.2.2</proguardVersion>
                <injar>${project.build.finalName}.jar</injar>
                <outjar>${project.build.finalName}.jar</outjar>
                <obfuscate>true</obfuscate>
                <proguardInclude>proguard.cfg</proguardInclude>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>net.sf.proguard</groupId>
                    <artifactId>proguard-base</artifactId>
                    <version>6.2.2</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.dummy.test.MainClass</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And the proguard.cfg looks like:

-dontshrink
-dontoptimize
-useuniqueclassmembernames
-adaptclassstrings

-keep public class * extends org.springframework.boot.web.support.SpringBootServletInitializer
-keep public class * extends org.springframework.boot.loader.**
-keepclasseswithmembers public class * { public static void main(java.lang.String[]);}
-keepclassmembers enum * { *; }
-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 *;
}
-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.data.redis.RedisReactiveAutoConfiguration class *
-keep @org.springframework.stereotype.Repository class *
-allowaccessmodification
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,RuntimeVisibleAnnotations
-keepdirectories org.springframework.boot.autoconfigure
-keepnames class * implements java.io.Serializable
-keepclassmembers class * {
     @org.springframework.beans.factory.annotation.Autowired *;
}
## keep same
-keepclassmembernames class com.dummy.test.entity.** { *; }
## keep getters and setters for mail template
-keepclassmembers class * {
    *** get*();
    void set*(***);
}

Solution

  • It turns out, Proguard moves all the obfuscated files under BOOT-INF.

    Spring boot scans the main package but it can not find your obfuscated files, because they are not in the main package. So you need to give a proper package name to Proguard to move them somewhere under the main package.

    All you need to do is give a new package name like:

    -repackageclasses com.foo.bar.obfuscated