spring boot jar 分离自动部署脚本

背景

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

maven 配置分离jar包

xml 复制代码
 <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>

部署脚本

bash 复制代码
#!/bin/bash

AppName="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"

    # 定义一个内嵌的函数来远程查询 PID
    query() {
        ssh "${RemoteUser}@${RemoteHost}" "ps -ef | grep java | grep '$AppName' | grep -v grep | awk '{print \$2}'"
    }

    # 获取远程进程 ID
    PID=$(query)

    if [ -n "$PID" ]; then
        echo "$AppName is already running on $RemoteHost (pid: $PID)..."
    else
        echo "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/lib
    scp ruoyi-admin/target/smart_engineering_admin.jar root@140.143.209.33:smart_engineering/
}

stop() {
    echo "Stopping $AppName on $RemoteHost"

    # 定义一个内嵌的函数来远程查询 PID
    query() {
        ssh "${RemoteUser}@${RemoteHost}" "ps -ef | grep java | grep '$AppName' | grep -v grep | awk '{print \$2}'"
    }

    # 获取进程 ID
    PID=$(query)

    if [ -n "$PID" ]; then
        echo "$AppName (pid:$PID) is running. Sending TERM signal..."
        ssh "${RemoteUser}@${RemoteHost}" "kill -TERM $PID"

        # 等待进程退出
        while [ -n "$PID" ]; do
            sleep 1
            PID=$(query)
        done
        echo "$AppName has exited."
    else
        echo "$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
相关推荐
码事漫谈25 分钟前
从外行到AI指挥官:你必须掌握的五大「程序员思维」
后端
Moonbit26 分钟前
MoonBit 开发者激励计划开启|赢取价值 $20 Copilot 月卡权益!
后端
码事漫谈28 分钟前
通信的三种基本模式:单工、半双工与全双工
后端
Q_Q51100828532 分钟前
python+uniapp基于微信小程序团购系统
spring boot·python·微信小程序·django·uni-app·node.js·php
前端中后台35 分钟前
如何防止短信验证码接口被盗刷
后端
m0_736927041 小时前
Spring Boot自动配置与“约定大于配置“机制详解
java·开发语言·后端·spring
重生之我在二本学院拿offer当牌打1 小时前
秒杀场景下的MySQL优化:从崩溃到抗住100万QPS
后端
重生之我在二本学院拿offer当牌打1 小时前
IoC容器深度解析(三):Bean生命周期11步骤深度剖析,彻底搞懂Spring核心机制!
后端
重生之我在二本学院拿offer当牌打2 小时前
手写SpringBoot Starter(三):实现可插拔Starter,像Zuul一样优雅!
后端
重生之我在二本学院拿offer当牌打2 小时前
手写SpringBoot Starter(一):10分钟带你入门,从此告别重复配置!
spring boot