How to Shrink Spring Boot JARs: Remove Unused Libs for Faster Deployments
This guide explains why Spring Boot JARs become bulky when packaged with many dependencies, and provides a step‑by‑step method—including Maven commands and configuration changes—to strip out unnecessary lib files, dramatically reducing the final artifact size for smoother production deployments.
1. Introduction
Spring Boot is easy to deploy, but when the application runs on a public cloud server the generated JAR can become huge, especially if many libraries such as Spring Cloud are included. Large JARs make updates and deployments painful.
2. Jar Size Before Optimization
In a typical Spring Boot JAR, most of the disk space is taken by external dependency JARs located in BOOT-INF/lib. In the example shown, the whole JAR is 18.18 MB, while BOOT-INF/lib alone occupies almost 18 MB.
3. Solution
Step 1: Build the normal JAR and extract the lib folder
Run the standard Maven build: mvn clean install After the build, unzip the generated JAR and copy the BOOT-INF/lib directory to a temporary location.
Step 2: Modify pom.xml to exclude the lib folder from the final artifact
Adjust the Maven configuration (see the screenshot in the original article) so that the resulting JAR is built without embedding the external libraries. <!-- pom.xml snippet placeholder --> Run the build again: mvn clean install The new JAR size shrinks dramatically because the external JARs are no longer packaged.
Step 3: Run the optimized JAR
Place the previously extracted lib folder and the newly built slim JAR in the same directory, then start the application with: java -jar your‑app.jar Alternatively, you can use Maven to copy only the required JARs to a target directory with a command similar to the one shown in the article.
4. Final Directory Structure
After the process, the deployment directory contains the slim JAR and a separate lib folder with the needed dependencies, resulting in a much lighter package and faster deployment cycles.
5. Remarks
Once the project’s architecture is fixed, most dependencies remain constant, so future changes usually involve only business logic. Re‑building the slim JAR after code changes is quick, greatly improving deployment efficiency.
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 Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
