当前位置: 首页 > news >正文

spring boot jar 分离自动部署脚本

背景

远程部署时spring boot 包,比较大。可以采用依赖库和业务包分离的方式。提供一个脚本进行自动部署

maven 配置分离jar包

 <build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><!-- 导入系统路径包 --><includeSystemScope>true</includeSystemScope><includes><include><groupId>nothing</groupId><artifactId>nothing</artifactId></include></includes></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><!-- 生成的jar中不要包含pom.xml和pom.properties这两个文件 --><addMavenDescriptor>false</addMavenDescriptor><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><!--这里需要修改为你的项目的主启动类--><mainClass>com.ruoyi.RuoYiApplication</mainClass></manifest></archive></configuration></plugin><!--设置将 lib 拷贝到应用 Jar 外面--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy-lib</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>target/lib</outputDirectory><excludeTransitive>false</excludeTransitive><stripVersion>false</stripVersion><includeScope>runtime</includeScope></configuration></execution></executions></plugin></plugins></build>

部署脚本

#!/bin/bashAppName="smart_engineering_admin.jar"
RemoteUser="root"
RemoteHost="xxx.xxx.xxx.xxx"
JVM_OPTS=" -Dloader.path=smart_engineering/lib -Dserver.port=8080 -Dname=$AppName  -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps  -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"start() {echo "Starting $AppName on $RemoteHost"# 定义一个内嵌的函数来远程查询 PIDquery() {ssh "${RemoteUser}@${RemoteHost}" "ps -ef | grep java | grep '$AppName' | grep -v grep | awk '{print \$2}'"}# 获取远程进程 IDPID=$(query)if [ -n "$PID" ]; thenecho "$AppName is already running on $RemoteHost (pid: $PID)..."elseecho "Starting $AppName on $RemoteHost..."ssh "${RemoteUser}@${RemoteHost}" "nohup java $JVM_OPTS -jar smart_engineering/$AppName > /dev/null 2>&1 &"echo "$AppName started successfully on $RemoteHost."ssh "${RemoteUser}@${RemoteHost}" "tail -f logs/sys-console.log"fi
}sync() {rsync -avc --delete ruoyi-admin/target/lib/ root@140.143.209.33:smart_engineering/libscp ruoyi-admin/target/smart_engineering_admin.jar root@140.143.209.33:smart_engineering/
}stop() {echo "Stopping $AppName on $RemoteHost"# 定义一个内嵌的函数来远程查询 PIDquery() {ssh "${RemoteUser}@${RemoteHost}" "ps -ef | grep java | grep '$AppName' | grep -v grep | awk '{print \$2}'"}# 获取进程 IDPID=$(query)if [ -n "$PID" ]; thenecho "$AppName (pid:$PID) is running. Sending TERM signal..."ssh "${RemoteUser}@${RemoteHost}" "kill -TERM $PID"# 等待进程退出while [ -n "$PID" ]; dosleep 1PID=$(query)doneecho "$AppName has exited."elseecho "$AppName is already stopped."fi
}sync
stop
sleep 2
start
  • 优点
    • 依赖包不变化不会同步,减少上传的文件
    • 每次都同步业务包,保证准确性
    • 提升发布速度

要求

  • windows 的wsl下可用
  • 需要和服务器配置ssh互信

进一步封装

#!/bin/bash./mvnw -T 1C clean package -P prod./push_run.sh
http://www.lryc.cn/news/450499.html

相关文章:

  • PGMP-03战略一致性
  • 华为OD机试真题---智能成绩表
  • 828华为云征文 | 华为云Flexus云服务器X实例搭建企业内部VPN私有隧道,以实现安全远程办公
  • Hadoop集群的高可用(HA):NameNode和resourcemanager高可用的搭建
  • 支付宝沙箱环境 支付
  • 获取unity中prefab的中文文本内容以及和prefab有关的问题
  • Web自动化中常用XPath定位方式
  • Unity3D播放GIF图片使用Animation来制作动画
  • redo log 和 bin log 的两阶段提交
  • Go基础学习07-map注意事项;多协程对map的资源竞争;sync.Mutex避免竟态条件
  • 远程服务器安装anaconda并创建虚拟环境
  • 什么是IIC通信协议?
  • P3131 [USACO16JAN] Subsequences Summing to Sevens S Python题解
  • 鸿蒙NEXT开发-ArkUI(基于最新api12稳定版)
  • Matplotlib 使用 LaTeX 渲染图表中的文本、标题和数学公式
  • Android 安卓内存安全漏洞数量大幅下降的原因
  • c++primier第十二章类和动态内存
  • Ansible学习之ansible-pull命令
  • Linux:磁盘管理
  • FP7209: 用于紫外线消毒灯的 升压LED恒流驱动芯片
  • 【华为HCIP实战课程二】OSPF基础介绍和OSPF RID NBMA配置详解
  • 网络编程(13)——单例模式
  • 基于定制开发与2+1链动模式的商城小程序搭建策略
  • 银河麒麟,apt 安装软件报错640Unknown Status
  • python UNIT 3 选择与循环(2)
  • 828华为云征文|部署在线文档应用程序 CodeX Docs
  • Linux的多线程(线程的创建,退出,取消请求,取消处理例程,线程属性的设置)
  • git 本地代码关联远程仓库并推送
  • 推荐一个可以把PDF样本册转换为翻页电子书的网站
  • 【Linux 23】线程池