Spring Boot应用部署 - JAR包部署瘦身

瘦身部署方案

说明

在日常开发测试环境中,随着业务代码的增加,Maven依赖的jar包也越来越多,导致工程包越来越大。通过阿里云流水线部署时,构建时长逐渐变得不够使用。为了解决这个问题,我们提出了以下瘦身部署方案。

执行

2.1 工程打包jar文件,解压取出非工程依赖的jar(BOOT-INF/lib)

在构建过程中,首先将工程打包为jar文件,然后解压该jar文件,取出其中的非工程依赖的jar包(通常位于BOOT-INF/lib目录下)。

2.2 修改工程pom.xml

修改前
xml 复制代码
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.3.2.RELEASE</version>
</plugin>
修改后
xml 复制代码
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.3.2.RELEASE</version>
    <configuration>
        <mainClass>xxx.xxx.Application</mainClass>
        <layout>ZIP</layout>
        <includes>
            <!-- 内部依赖 -->
            <include>
                <groupId>xxx</groupId>
                <artifactId>xxx</artifactId>
            </include>
            <include>
                <groupId>xxx</groupId>
                <artifactId>xxx</artifactId>
            </include>
            <!-- 其他内部依赖... -->
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

注意 :在<includes>标签中,需要列出所有需要包含在最终jar包中的内部依赖。每次新增Maven依赖时,需要手动更新这个列表。

2.3 部署脚本,增加参数【Dloader.path】

在部署脚本中,需要为Java命令添加-Dloader.path参数,指定非工程依赖jar包的目录。

bash 复制代码
nohup java -Xms2g -Xmx2g -server -XX:+HeapDumpOnOutOfMemoryError -Dspring.profiles.active=xxx -Dloader.path=${APP_HOME}/lib -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 &

在这个命令中,${APP_HOME}/lib是非工程依赖jar包的目录,${JAR_NAME}是瘦身后的工程jar包的名称。${JAVA_OUT}是Java输出日志的文件名。

相关推荐
暮色妖娆丶8 小时前
SpringBoot 启动流程源码分析 ~ 它其实不复杂
spring boot·后端·spring
消失的旧时光-19438 小时前
第十四课:Redis 在后端到底扮演什么角色?——缓存模型全景图
java·redis·缓存
BD_Marathon8 小时前
设计模式——依赖倒转原则
java·开发语言·设计模式
BD_Marathon8 小时前
设计模式——里氏替换原则
java·设计模式·里氏替换原则
Coder_Boy_8 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例中相关概念
java·人工智能·spring boot·后端·spring
css趣多多8 小时前
add组件增删改的表单处理
java·服务器·前端
雨中飘荡的记忆8 小时前
Spring Batch实战
java·spring
Java后端的Ai之路8 小时前
【Spring全家桶】-一文弄懂Spring Cloud Gateway
java·后端·spring cloud·gateway
devmoon8 小时前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
野犬寒鸦8 小时前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习