PulsarRPAPro
PulsarRPAPro copied to clipboard
Reduce the size of the standalone jar
To reduce the size of a standalone JAR (also known as a "fat JAR" or "uber JAR") in a Spring Boot or other Java-based application, consider the following strategies:
1. Use Spring Boot Thin Launcher
- Instead of packaging dependencies inside the JAR, use the Thin Launcher (
org.springframework.boot.experimental:spring-boot-thin-launcher). - This allows dependencies to be downloaded at runtime, significantly reducing the initial JAR size.
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-launcher</artifactId>
<version>1.0.28.RELEASE</version>
</dependency>
Run with:
java -jar yourapp.jar
This method requires an internet connection on the first run.
2. Exclude Unused Dependencies
- Remove unnecessary dependencies from
pom.xml(Maven) orbuild.gradle(Gradle). - Use dependency analysis tools to detect unused dependencies:
mvn dependency:analyze
3. Use ProGuard (for Code Shrinking & Obfuscation)
- ProGuard removes unused code and reduces class sizes.
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
</configuration>
</plugin>
4. Use the spring-boot-maven-plugin Layered JAR Feature
- Exclude development dependencies and optional libraries.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layers>
<enabled>true</enabled>
</layers>
</configuration>
</plugin>
Build with:
mvn clean package
5. Use maven-shade-plugin with Filters
- Exclude unnecessary classes and resources from dependencies.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<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>
6. Use GraalVM Native Image
- Compile the JAR to a native executable, removing unused classes and reducing size.
native-image -jar yourapp.jar yourapp
This will create a much smaller, optimized binary.
7. Compress the JAR using Pack200 (Deprecated)
pack200 --gzip yourapp.jar.pack.gz yourapp.jar
This is deprecated in Java 14+, but still works for legacy apps.
8. Strip Unnecessary Classes & Resources
- Manually inspect the
target/dependencyfolder and remove large unused libraries. - Use class data sharing (CDS) or jlink (for JDK 9+ modular applications).
Conclusion
The best approach depends on your needs:
- Minimal download size? → Thin Launcher
- Remove unused classes? → ProGuard or GraalVM
- Remove unnecessary dependencies? →
mvn dependency:analyze - Optimize for performance? → Native Image
Would you like help with a specific method? 🚀