Original address: Spring Boot project Jar package slimming
1. Jar bag before weight loss
In the Jar package compiled by Spring Boot, some external dependent libraries (jar packages) occupy a large disk space. For example, enter the project root directory and execute the mvn clean install command to open the Jar package with compressed software. The directory structure as follows:
The jar package size of the entire project is 111 MB, but the imported external dependency Jar alone accounts for 99M. Because the external dependencies of the project will not change under normal circumstances, the external dependencies can be independent.
Second, the method
Step 1: Compile the JAR package normally, unzip the lib folder
POM
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.XXXApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Step 2: Modify the pom.xml configuration to compile the Jar package without the lib folder
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.MxemApplication</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>
Step 3: Run the compiled Jar package
java -Dloader.path=/path/to/lib -jar /path/to/springboot-xxx-0.0.1-SNAPSHOT.jar
Remarks:
- Change /path/to/ to the actual path.
- -Dloader.path=lib folder path