How to Slim Down SpringBoot Jar Packages for Faster Deployment
This article explains how to reduce the size of SpringBoot executable JAR files by separating application classes from dependency libraries, configuring the Maven build to use a ZIP layout, and deploying the slimmed package with a custom loader path, thereby speeding up uploads and updates.
Idea
Analyzing a SpringBoot JAR reveals three main sections: BOOT-INF , META-INF , and org . Inside BOOT-INF you will find classes (your compiled code) and lib (all Maven dependencies). The classes folder is usually small (around 3 MB) while the lib folder can be large (around 70 MB), making uploads slow.
Therefore, you can separate your own code from the third‑party JARs and only upload the code part for each iteration.
Slim Deployment
1. Normal Packaging
In the project's pom.xml the default packaging is defined as:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Build the project to obtain a JAR, unzip it (or rename the suffix to .zip ), and extract the BOOT-INF/lib directory.
2. Change Packaging Configuration
Modify the Maven plugin configuration to produce a slimmer package:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.zyxx.DeclareApplication</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>mainClass : specifies the entry point of the application.
layout : sets the packaging format to ZIP (must be uppercase).
includes : allows you to add your own dependency JARs if needed.
repackage : removes unnecessary dependencies, keeping only the essential structure.
3. Re‑package
Run mvn package again. The resulting JAR will be only a few megabytes in size.
Upload and Start
Upload the lib directory together with the newly built slim JAR to the server.
Start the application with the following command:
nohup java -Dloader.path=./lib -jar ./sbm-0.0.1-SNAPSHOT.jar &-Dloader.path : tells SpringBoot where to find the external dependency JARs.
sbm-0.0.1-SNAPSHOT.jar : the name of your slimmed application JAR.
nohup and & : run the process in the background.
Summary
Using the slim deployment approach allows you to update the application without re‑uploading a large JAR each time, significantly reducing deployment time.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.