Optimizing Spring Boot Jar Packaging: Reducing Deployment Size with Dependency Separation
This article explains how to shrink large Spring Boot fat JARs and microservice deployment packages by separating dependencies into a shared lib directory, using Maven plugins and manifest configuration across four progressive levels, ultimately achieving kilobyte‑scale business JARs and faster deployments.
With the popularity of Spring Boot, a single executable JAR can easily grow to hundreds of megabytes, and a microservice system may require several gigabytes of deployment files, causing slow updates and bandwidth issues.
The guide presents a step‑by‑step "fat‑jar slimming" process divided into four levels, each demonstrated with Maven configuration snippets and command‑line output.
Level 0 – Conventional Fat JAR
Standard build produces a large JAR (tens to hundreds of MB). Example
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>and the resulting JAR size is shown with
ls -lh package-optimize-app1/target/package-optimize-app1.jar.
Level 1 – Separate Dependencies into lib Directory
Uses maven-dependency-plugin to copy all dependency JARs to target/lib and configures spring-boot-maven-plugin with an empty <includes> list so the main JAR contains only the application code (≈150 KB). The runtime command becomes java -jar -Djava.ext.dirs=lib package-optimize-app1.jar, achieving near‑instant deployment.
Level 2 – Merge All Modules' Dependencies into a Single lib Directory
Adds maven-jar-plugin to generate a MANIFEST with Class-Path: lib/…, then copies dependencies of all microservices into a common devops/lib folder. The resulting microservice JARs remain ~150 KB while the shared lib totals only a few hundred MB, eliminating the need for -Djava.ext.dirs at runtime.
Level 3 – Include System‑Scope Third‑Party SDKs
Handles non‑Maven SDK JARs referenced with system scope by adding a custom Class-Path entry via manifestEntries. Example property jar-manifestEntries-classpath=. lib/hik-sdk-1.0.0.jar ensures the SDK is loaded without Maven repository support.
All levels share the same build structure: define <finalName>, configure plugins, and run mvn clean install. The final deployment layout places every microservice JAR (≈150 KB) and the shared lib directory (≈200‑300 MB) under a common devops folder.
Final Results
All services share a unified dependency directory, reducing total deployment size to a few hundred megabytes.
Individual service JARs are reduced to a few hundred kilobytes, enabling rapid bug‑fix releases.
Explicit MANIFEST Class-Path entries prevent version conflicts between microservices.
System‑scope third‑party SDKs are correctly referenced and packaged.
For projects with changing Maven dependencies, remember to sync updated JARs to the shared lib directory; committing the deployment resources to Git can help track changes and minimize transfer size.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
