The main points of this film
- Introduce how to do for jar Bag slimming , Easy to deploy .
The normal way to package deployment
The detailed deployment process has been described in the article before ：SpringBoot Project deployment to Linux Server and publish .
however , There are some problems with this approach , Every time I upload jar It takes a long time to get the package to the server . Why? ？ Maybe we just created a basic SpirngBoot project , Maybe we just wrote three or four lines of code , Last generated jar The size of the bag is ridiculous , A good 17M, The reason is that the generated executable jar Not only the compiled classes , It also includes the information that the program depends on jar package , And that's the majority of that jar package .
We look at the build through compression software jar Contents of the package ：
slimming.jar # 17M - BOOT-INF - classes # Store compiled code - lib # Run the required dependencies - classpath.idx - META-INF - org
We can clearly see that , Lead to jar This is the culprit for the size of the bag lib, And often we rarely change these dependencies , What changes frequently is the logic in some code , therefore , The idea of slimming is ： take lib from jar Take it out , Just upload once lib, The volume of the rest is negligible , It can be transmitted in seconds .
Get lib Catalog
According to the original maven Configuration mode , stay target Generate under directory jar.
<build> <finalName>slimming</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
$ mvn clean package
Get BOOT-INF/lib Catalog .
Change the default packaging method
<build> <finalName>slimming</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- Specifies the startup class of the project --> <mainClass>com.hyh.SpringBootSlimmingApplication</mainClass> <!-- Specify the packing method as ZIP --> <layout>ZIP</layout> <includes> <!-- If you have your own dependence jar, You can import --> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <!-- Eliminate other dependencies , Keep only the simplest structure --> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
$ mvn clean package
/target In the catalog slimming.jar Has gone from 18000K become 121K.
Upload lib and jar
- lib - slimming.jar
$ nohup java -Dloader.path=./lib -jar slimming.jar $
- Dloader.path： Inform the project that it depends on jar Where is the bag .
The content of this article is a summary of excellent blogs and official documents , The address of the original text has been marked in the reference section of the text . Last , All the code samples in this article have been uploaded to Gitee：https://gitee.com/tqbx/springboot-samples-learn, There are other SpringBoot The integration of oh .