How to Slim Down SpringBoot JARs: Remove Unused Libraries for Faster Deployments
This guide explains why SpringBoot JARs become bulky when many dependencies are included, shows how to inspect the packaged libraries, and provides step‑by‑step Maven configurations and runtime commands to build a leaner JAR that loads external libs only when needed, dramatically speeding up deployment.
Introduction
Deploying a SpringBoot application is straightforward, but when the service runs on a public cloud the generated JAR can become huge because it bundles all external dependencies (e.g., Spring Cloud). Adjusting the running application then becomes painful.
Jar size before slimming
In a typical build the JAR may be around 18 MB, with the BOOT-INF/lib directory consuming almost the entire size.
Solution
Step 1: Build the normal JAR and extract the lib folder
Use the standard Maven build and then unzip the resulting JAR to obtain the BOOT-INF/lib directory.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.johnnian.App</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>Run mvn clean install, then copy the BOOT-INF/lib folder to the target directory.
Step 2: Modify pom.xml to produce a JAR without the embedded libraries
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.johnnian.App</mainClass>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>After updating the configuration, run mvn clean install again. The new JAR is much smaller because external JARs are no longer packaged.
Step 3: Run the slim JAR with external libraries
Place the previously extracted lib folder and the newly built JAR in the same directory, then start the application with:
java -Dloader.path=/path/to/lib -jar /path/to/springboot-jsp-0.0.1-SNAPSHOT.jarAlternatively, you can copy the required dependencies using Maven:
mvn dependency:copy-dependencies -DoutputDirectory=F:\ideaWorkPlace\AnalysisEngine\lib -DincludeScope=runtimeReplace /path/to with the actual path of your lib directory. The final directory layout looks like:
├── lib # external libraries
└── springboot-jsp-0.0.1-SNAPSHOT.jarConclusion
Once the project’s architecture is fixed, the set of external JARs rarely changes; most future changes are in business logic. By rebuilding only the lightweight JAR, deployment becomes faster and more efficient.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
