1、问题说明
SpringBoot 部署起来虽然简单,如果服务器部署在公司内网,速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼:编译出来的 Jar 包很大,如果工程引入了许多开源组件(SpringCloud 等),那就更大了。这个时候如果想要对线上运行工程有一些微调,则非常麻烦。
2、瘦身前的 Jar 包
Tomcat 在部署 Web 工程的时候,可以进行增量更新,SpringBoot 也是可以的~
SpringBoot 编译出来的 Jar 包中,磁盘占用大的,是一些外部依赖库(jar 包),例如:
进入项目工程根目录,执行 mvn clean install 命令,得到的 Jar 包,用压缩软件打开,目录结构如下:
整个 Jar 包 16.2 MB, 但是 BOOT-INF/lib 就占用了将近 16.1 MB:
3、解决方法
3.1、步骤 1: 正常编译 JAR 包,解压出 lib 文件夹
POM 文件如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myron.demo.DemoApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
进入项目根目录,执行命令:mvn clean install
将编译后的 Jar 包解压,拷贝 BOOT-INF 目录下的 lib 文件夹 到目标路径;
3.2、步骤 2: 修改 pom.xml 配置,编译出不带 lib 文件夹的 Jar 包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myron.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>
配置完成后,再次执行编译:mvn clean install
生成的 Jar 包体积明显变小,如下所示, 外部的 jar 包已经不会被引入了:
3.3、步骤 3: 运行编译后的 Jar 包
将 步骤 1 解压出来的 lib 文件夹、步骤 2 编译的 jar 包放在同一个目录, 运行下面命令:
// pub1:/home/myron/springboot-helloworld % java -jar -Dloader.path=/home/myron/springboot-helloworld/lib /home/myron/springboot-helloworld/springboot-helloworld-0.0.1-SNAPSHOT.jar
或者在 maven 中输入一下命令导出需要用到的 jar 包
mvn dependency:copy-dependencies -DoutputDirectory=C:\Users\Administrator\Desktop\lib -DincludeScope=runtime
备注:
将 /home/myron/springboot-helloworld/ 改成实际的路径。
-Dloader.path=lib 文件夹路径
最终目录文件结构是:
├── lib #lib文件夹
└── springboot-helloworld-0.0.1-SNAPSHOT.jar
3.4、运行结果
1)代码:
2)pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.myron</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> -->
<!-- 步骤 1: 正常编译 JAR 包,解压出 lib 文件夹 -->
<!-- <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myron.demo.DemoApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> -->
<!-- 步骤 2: 修改 pom.xml 配置,编译出不带 lib 文件夹的 Jar 包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myron.demo.DemoApplication</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>
</project>
3)发送测试请求:
4)后台日志:
说明
- 1、通常,一个工程项目架构确定后,引入的 jar 包基本上不会变,改变的大部分是业务逻辑;
- 2、后面如果需要变更业务逻辑,只需要轻量地编译工程,大大提高项目部署的效率。