springboot 部署项目打jar太大,lib目录分离的压缩jar方法
一、解决问题
在项目部署时,经常发现打包后的 jar 很大,动不动就几十MB甚至 上百MB 大小, 每次该一点小的问题都需要极长的时间来更新服务
为了让更新服务更快速,于是我们我们需要压缩 jar 来处理这个问题
二、解决方法
使用 lib 目录的外部依赖jar 与当前服务程序的jar 进行分离,先把lib的依赖上传到服务,每次更新就不需要重新传lib的依赖了, 在启动时进行指定目录加载外部依赖jar 即可
2.1、在maven中的 build 中添加插件
<!-- ==================== 依赖jar 优化start ========================== -->
<!-- lib包,打完一次后可以注释掉也可以不管,如果有新引入的jar包需要把lib包下的jar更新到服务器下,
// 压缩后jar包的启动指令 nohup java -Dloader.path="lib/" -jar xxx.jar &
// 未压缩jar包的启动指令 nohup java -jar xxx.jar &"
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 依赖包输出目录,将来不打进jar包里 -->
<outputDirectory>${ project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<!-- 压缩jar包,打出来的jar中没有了lib文件夹 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!-- ==================== 依赖jar 优化end ========================== -->
2.2、执行maven 的打包命令 install
打包后获得 lib 目录和jar
我们去文件中可以看到打包后的当前服务的 jar 是非常小的,lib 下的外部jar 文件相对偏大
3、 lib 目录上传和更新
把lib 目录上传到 linux 服务器上 ,启动 jar 指定lib 目录即可
4、启动 jar
压缩后jar包的启动指令:
## 指定lib 目录启动
nohup java -Dloader.path="lib/" -jar xxx.jar &
未压缩jar包的启动指令:
nohup java -jar xxx.jar &
- 个人开源项目(通用后台管理系统)–> https://gitee.com/wslxm/spring-boot-plus2 , 喜欢的可以看看
- 本文到此结束,如果觉得有用,动动小手点赞或关注一下呗,将不定时持续更新更多的内容…,感谢大家的观看!
还没有评论,来说两句吧...