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.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Slim Down SpringBoot JARs: Remove Unused Libraries for Faster Deployments

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.jar

Alternatively, you can copy the required dependencies using Maven:

mvn dependency:copy-dependencies -DoutputDirectory=F:\ideaWorkPlace\AnalysisEngine\lib -DincludeScope=runtime

Replace /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.jar

Conclusion

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavamavenspringbootJar Optimization
Java High-Performance Architecture
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.