Spring Boot project slimming guide, incredibly thin!

Python combat community

Java combat community

Long press to identify the QR code below, add as required

Scan QR code to follow to add customer service

Enter the Python community▲

Scan QR code to follow to add customer service

Enter the Java community

table of Contents:

1. Introduction
2. Jar package before slimming
3. Solution

I. Introduction

Although Spring Boot is simple to deploy, if the server is deployed on the company's intranet, the speed is okay, but if it is deployed on the public network, deployment is really a headache: the compiled Jar package is very large, if the project introduces many open source components ( Spring Cloud, etc. ), it's even bigger.

At this time, it is very painful if you want to make some fine-tuning of the online running project.

2. Jar bag before weight loss

Tomcat can perform incremental updates when deploying Web projects, and Spring Boot is also possible~

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. The obtained Jar package can be opened with compressed software. The directory structure as follows:

The entire Jar package is 18.18 MB, but BOOT-INF/lib occupies nearly 18 MB:

Three, the solution

Step 1: Compile the JAR package normally and extract the lib folder

The POM file is as follows:

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

Enter the project root directory and execute the command: mvn clean install

Unzip the compiled Jar package and copy the lib folder in the BOOT-INF directory to the target path;

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.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 the configuration is complete, execute the compilation again: mvn clean install

The generated Jar package is significantly smaller. As shown below, the external jar package has not been introduced:

Step 3: Run the compiled Jar package

Put the lib folder decompressed in step 1 and the jar package compiled in step 2 in the same directory, and run the following command:

java -Dloader.path=/path/to/lib -jar /path/to/springboot-jsp-0.0.1-SNAPSHOT.jar

Or enter the command in maven to export the jar package you need

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

Remarks:

Change /path/to/ to the actual path.

-Dloader.path=lib folder path

The final directory file structure is:

├── lib   #lib文件夹  
└── springboot-jsp-0.0.1-SNAPSHOT.jar

Description:

1. Generally, after the structure of an engineering project is determined, the imported jar package will basically not change, and most of the changes are business logic;

2. If you need to change the business logic later, you only need to compile the project lightly, which greatly improves the efficiency of project deployment.

Copyright statement: This article is the original article of the CSDN blogger "yjgithub". It follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.

Original link:

https://blog.csdn.net/yjgithub/article/details/80475521

程序员专栏 扫码关注填加客服 长按识别下方二维码进群

Recommended recent exciting content:  

 Comparison of programmer income in China, the United States, Japan and India

 A sad day for programmers

 SringMVC from entry to source code, this one is enough

 10 Python visual animations, carefully and beautifully


Watch the good article here to share it with more people↓↓

Guess you like

Origin blog.csdn.net/Px01Ih8/article/details/109268604