springboot项目通过ssh实现远程自动部署
- springboot项目通过ssh实现远程自动部署
-
- 在项目pom.xml文件中引入ssh上传jar包
- 引入wagon-maven-plugin插件
- 远程启动服务脚本
- 监控服务脚本
- 定时任务脚本
- 打包上传
- 执行启动命令
- 打包错误
-
- 执行上传命令错误
- 执行shell脚本错误integer expression expected
- 执行脚本错误nohup: failed to run command 'java': No such file or directory
- 远程启动不能链接服务器Failed to execute goal org.codehaus.mojo:wagon-maven-plugin:1.0:upload-single (default-cli) on project daison-server: Unable to create a Wagon instance for scp://root:[email protected]:22/home/daison/admin: Cannot connect. Reason: SSH_MSG_DISCONNECT: 2 Too many authentication failures -> [Help 1]
springboot项目通过ssh实现远程自动部署
在项目中,经常需要发布jar包、war包到服务器,通常我们使用本地编译,打包,然后将打成的包手动上传到服务器,操作非常麻烦。该文章记录windows本地通过命令执行打包后直接上传服务器.
在项目pom.xml文件中引入ssh上传jar包
<!--配置wagon-ssh连接服务器 ssh 依赖包-->
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.8</version>
引入wagon-maven-plugin插件
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
详细配置
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--公用jar-->
<jsch.version>0.1.54</jsch.version>
<log-elk.version>1.6</log-elk.version>
<javamelody.version>1.72.0</javamelody.version>
<itext.version>2.1.7</itext.version>
<!--wagon plugin 配置-->
<service-path>/home/daison</service-path>
<pack-name>${project.artifactId}-${project.version}.jar</pack-name>
<remote-addr>192.168.1.110:22</remote-addr>
<remote-username>root</remote-username>
<remote-passwd>123456</remote-passwd>
<!--
<remote-addr>192.168.1.150:22</remote-addr>
<remote-username>root</remote-username>
<remote-passwd>123456</remote-passwd>
-->
</properties>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<extensions>
<extension>
<!--配置wagon-ssh连接服务器 ssh 依赖包-->
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.8</version>
</extension>
</extensions>
<!--maven打包插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<!-- 跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--上传插件-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<!-- serverId即在settings.xml中配置的service的id -->
<!-- <serverId>linux-server-dev</serverId>-->
<!-- 需要部署的文件 -->
<fromFile>target/${pack-name}</fromFile>
<!-- 部署目录 用户:密码@ip+部署地址:端口 -->
<url><![CDATA[ scp://${remote-username}:${remote-passwd}@${remote-addr}${service-path} ]]></url>
<!--shell 执行脚本 -->
<!-- <commands>
<!– 停止服务–>
<command>sh /home/daison/admin/script/beacon-server.sh stop</command>
<!– 启动服务 –>
<command>sh /home/daison/admin/script/beacon-server.sh start</command>
</commands>-->
<commands>
<!-- Kill Old Process -->
<command>kill -9 `ps -ef |grep ${
project.build.finalName}.jar|grep -v "grep" |awk '{print $2}'`</command>
<!-- Restart jar package,write result into renren.log -->
<command>
<![CDATA[nohup java -jar ${service-path}/${pack-name} --spring.profiles.active=prod > ${service-path}/server.log 2>&1 & ]]></command>
<command><![CDATA[netstat -nptl]]></command>
<command><![CDATA[ps -ef | grep java | grep -v grep]]></command>
</commands>
<!-- 运行命令 mvn clean package wagon:upload-single wagon:sshexec-->
<!-- 显示运行命令的输出结果 -->
<displayCommandOutputs>true