Reducing SpringBoot Jar Size by Excluding Unnecessary lib Dependencies
This guide explains why SpringBoot JAR files become large when bundled with many external libraries, and provides a step‑by‑step method—including Maven configuration changes and runtime loader settings—to shrink the JAR by removing the BOOT‑INF/lib folder while preserving application functionality.
When a SpringBoot application is deployed to a public cloud server, the generated JAR often exceeds 18 MB because the BOOT‑INF/lib directory contains all external dependencies, which makes incremental updates and runtime tuning difficult.
Step 1: Build the project normally with mvn clean install , unzip the resulting JAR, and copy the BOOT‑INF/lib folder to the target machine.
Step 2: Modify pom.xml to repackage the JAR without the lib folder. Example configuration:
<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 POM, run mvn clean install again; the new JAR will be dramatically smaller because external JARs are no longer packaged.
Step 3: Run the reduced JAR by specifying the external lib directory with the SpringBoot loader path:
java -Dloader.path=/path/to/lib -jar /path/to/springboot-jsp-0.0.1-SNAPSHOT.jarAlternatively, you can copy required dependencies using Maven:
mvn dependency:copy-dependencies -DoutputDirectory=F:\ideaWorkPlace\AnalysisEngine\lib -DincludeScope=runtimeFinal directory layout should look like:
├── lib # external libraries
└── springboot-jsp-0.0.1-SNAPSHOT.jarNotes: the /path/to placeholder must be replaced with the actual file system path. Once the architecture is fixed, most JARs remain unchanged, and only business logic needs recompilation, greatly improving deployment efficiency.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.