Directory:

1. Introduction

2. Jar packages before slimming down

3. Solutions

One, foreword

Spring Boot is easy to deploy, and it’s ok if the server is deployed on the Intranet, but it’s a pain to deploy on the public network: the compiled Jar packages are large, especially if the project introduces many open source components (such as the Spring Cloud).

At this point, if you want to do some fine tuning of the online running project, it can be very painful.

Two, the Jar package before slimming

Incremental updates are available for Tomcat Web projects, and Spring Boot is also available

For example, enter the root directory of the project and run the MVN clean install command to obtain the Jar package. Open the Jar package using the compression software. The directory structure is as follows:

The entire Jar package is 18.18 MB, but boot-INF /lib takes up nearly 18 MB:

Third, solutions

Step 1: Compile the JAR package and decompress the lib folder

POM files are 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>Copy the code

Go to the root directory of the project and run the MVN clean install command

Decompress 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>Copy the code

After the configuration is complete, run MVN Clean Install again

The size of the generated Jar package is significantly smaller, as shown below. External JARS are no longer imported:

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:

 
Copy the code
Java - Dloader. Path = / path/to/lib jar/path/to/springboot - JSP - 0.0.1 - the SNAPSHOT. The jarCopy the code

Or enter a command in Maven to export the required JAR packages

mvn dependency:copy-dependencies -DoutputDirectory=F:\\ideaWorkPlace\\AnalysisEngine\\lib -DincludeScope=runtime   
Copy the code
Remark:Copy the code

Change /path/to/ to the actual path.

-dloader. path= Lib folder path

The final directory file structure is:

├─ ├─ ├─ ├─ Springboot-jsp-0.0.1-snapshot.jarCopy the code

Description:

1. Generally, after the architecture of an engineering project is determined, the introduced JAR package will not change basically, 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.