「2021.08 实战」Springboot2.x wagon 2.x 部署线上(自动重启)

参考:

  1. java项目自动部署方案(1):wagon-maven-plugin
  2. wagon-maven-plugin插件使用
  3. linux 查找并杀死进程
  4. MVN 仓库
  5. maven 插件之使用wagon-maven-plugin上传jar包到服务器上运行

简介

昨天转载了一篇文章 springboot 使用 wagon 持续集成(自动上传服务器,执行脚本(实现重启等操作)),不限于springboot,实测并不能够正常运行.

然后今天又踩了很多 [ bug ] 去解决.

开始

  1. 为了让wagon-maven-plugin插件能SSH连上Linux服务器,首先需要在Maven的配置文件settings.xml中配置好server的用户名和密码。
<servers>
    <server>
        <!-- 这个字段是为了在pom.xml中引入,可以是其他字符,比如ip或标识符 -->
        <!-- <id>xxx.xxx.xx.xxx</id> -->
        <id>linux-server-dev</id>
        <username>root</username>
        <password>123456</password>
    </server>
</servers>
  1. 在项目的pom.xml中的 dependencies 节点内加入
<dependencies>
<!--        持续集成(自动代码上传服务器)-->
<!--        别人文档都是引入这个,实测不引入也不报错-->
<!--        <dependency>-->
<!--            <groupId>org.apache.maven.wagon</groupId>-->
<!--            <artifactId>wagon-provider-api</artifactId>-->
<!--            <version>3.4.3</version>-->
<!--        </dependency>-->
        <!-- https://mvnrepository.com/artifact/org.codehaus.mojo/wagon-maven-plugin -->
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
<!--            这里的版本号要与下面要引入的 build 配置 版本相同-->
            <version>2.0.2</version>
        </dependency>
</dependencies>
  1. 在 pom.xml 中的 build/plugins 新增
         <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
<!--                这个版本号要与上面引入的版本号相同-->
                <version>2.0.2</version>
                <executions>
                    <execution>
                        <id>upload-and-deploy</id>
                        <!-- 插件在maven的package阶段执行,即打包后执行,实测没用 -->
                        <phase>package</phase>
                    </execution>
                </executions>
                <configuration>
<!--                    在 mvn 的 setting.xml 中对应的id-->
                    <serverId>xx.xxx.xx.xx</serverId>
<!--                    要上传的 jar 文件-->
                    <fromFile>target/${artifactId}-${version}.jar</fromFile>
<!--                    -->
                    <url>scp://ubuntu@xxx.xxx.x.xxx/home/springboot/moran</url>
<!--                        要执行的命令,按照顺序执行,-->
                    <commands>
                        <!-- 杀死原来的进程 -->
                        <command>sudo pkill -f ${artifactId}-${version}.jar</command>
<!--                        这边测试上传后可能会没有权限的bug,所以在这里给他授权一下-->
                        <command>sudo chmod -R 777 /home/springboot/xxx/${artifactId}-${version}.jar</command>
<!--                        启动jar包,并设置配置文件为线上的配置文件-->
<!--                        上传命令 mvn package wagon:upload-single wagon:sshexec-->
                        <command>java -jar /home/springboot/xxx/${artifactId}-${version}.jar --spring.profiles.active=prod</command>
                    </commands>
                    <!-- 显示运行命令的输出结果 -->
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>
  1. 在项目目录终端执行,就会自动上传到服务器并按照命令顺序执行
mvn clean package wagon:upload-single wagon:sshexec

总结

  1. 项目还是有一些问题,上传文件之后会显示命令执行,但是没有获取到结果,并且会卡住(但不会影响程序的启动与运行)
  2. wagon-maven-plugin 1.x 版本需要配置 ssl ,因为我的是2.x版本,所以没有配置。(之前看别的教程都是1.x 总是报错)
  3. 上传配置不仅文章内的,还有很多其他选项
  4. markdown 很炫酷
posted @ 2021-08-03 13:27  夏秋初  阅读(144)  评论(0编辑  收藏  举报