preface
If the project depends on too much, the network bandwidth is too small, sending packets to local build upload and so on, these problems very affect the release efficiency (yes, a company network bandwidth is too small, upload a 300M package takes 20 minutes, who can stand). Although there will be a lot of dependencies in Java projects, but when the version iteration is stable, the dependencies will basically not change, if you can put these unchanged dependencies on the server in advance, ignore these dependencies when packaging, then the Jar package will be much smaller, directly improve the efficiency of the release.
New project
Springboot-slimming is a POM project, server is a Springboot project, server depends on core, simulation of actual production projects.
Add common-LANG3 dependencies to the core project
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
Copy the code
Create a new test tool class
public class TestUtils {
public static String getUUID(a) {
return RandomStringUtils.random(100); }}Copy the code
Create a new TestController in the server
@RestController
public class TestController {
@GetMapping(value = "/test")
public Object test(a) {
returnTestUtils.getUUID(); }}Copy the code
Now that the simulation project is complete, look at the size of the packaged JAR package.
There are 16.8 M.
Dependencies take up most of the space in the project.
Thin body
Here’s how to lose weight:
Edit the pom
Modify poM file, add configuration under spring-boot-maven-plugin:
(1) Specify the dependencies that are included, and exclude those that are not.
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<artifactId>load_lib_test</artifactId>
<groupId>test</groupId>
</include>
</includes>
</configuration>
Copy the code
② Specify dependencies that are not included. Separate multiple groupids from each other
<configuration>
<layout>ZIP</layout>
<excludeGroupIds>
com.google.api-ads,
com.google.api-client,
org.apache.commons
</excludeGroupIds>
</configuration>
Copy the code
We use method one, add projects that depend on other projects of our own, and don’t put any third party dependencies into jar packages.
Final configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<artifactId>core</artifactId>
<groupId>cn.happyjava</groupId>
</include>
</includes>
<! -- <excludeGroupIds>-->
<! -- com.google.api-ads,-->
<! -- com.google.api-client,-->
<! -- org.apache.commons-->
<! -- </excludeGroupIds>-->
</configuration>
</plugin>
</plugins>
</build>
Copy the code
repack
This time it’s only 149KB.
Looking at the JAR, you are left with your own core dependency and a SpringBoot dependency
But this jar package cannot be run directly with Java-JAR.
Running it directly prompts ClassNotFoundException.
Export the required dependencies for the project
Go to the server directory address and run the following command
mvn dependency:copy-dependencies -DoutputDirectory=e:/lib
Copy the code
All dependencies in the project are exported (except those we ignore)
Run load JAR package
Export jar package can be put on the server, later release can save a lot of time. At runtime, -dloader. path=”e:\lib” is added to load the JAR package.
Java - jar - Dloader. Path = "e: \ lib" server - 0.0.1 - the SNAPSHOT. The jarCopy the code
Start the project
The project can be started normally; The access interface is normal.
conclusion
This is about SpringBoot packaging slim introduction, if the package is too large to affect the efficiency of the release, this is a good solution.
The source address
Github.com/happyjava00…