> 文章列表 > wagon-maven-plugin 实现远程部署知识回顾

wagon-maven-plugin 实现远程部署知识回顾

wagon-maven-plugin 实现远程部署知识回顾

背景

记录 maven 的 wagon-plugin 自动部署插件的两个问题点:

  1. 远程主机密码中有特殊字符 @ 直接在 url 路径的 scp 命令中写帐号密码识别不了的问题。
  2. 执行 Java 命令报 java: command not found 的问题。
  3. commands 命令集合中,多个 command 之间开启的是不同的会话,如果相互依赖的 Shell 命令只能放在同一个 command 标签中,命令之间用 ; 分割。

实践流程

wagon-maven-plugin 实现远程自动化知识回顾:

第一步,引入插件依赖,在 build 节点中配置 extensions 额外依赖:

<extensions><!--自动部署插件 wagon-ssh 依赖配置 --><extension><groupId>org.apache.maven.wagon</groupId><artifactId>wagon-ssh</artifactId><version>2.8</version></extension>
</extensions>

第二步,配置 maven server 主机信息,在 maven 的 setting.xml 配置文件的 servers 节点中,添加一个目标主机的配置信息,配置主机的帐号和密码。

<server><id>linux-server1</id><username>root</username><password><![CDATA[xxxxx特殊字符]]></password>
</server>

第三步,wagon 插件配置,指定 fromFile、url 、commands

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>wagon-maven-plugin</artifactId><version>2.0.2</version><configuration><serverId>linux-server1</serverId><fromFile>target/xxx.jar</fromFile><url>scp://IP/path/lib</url><!-- 在服务器执行的命令集合 --><commands><!-- 1、所有需要执行的命令,必须放在一个 command 中,不同 command 属于不同的 ssh session 了 --><!-- 2、远程执行shell脚本时,不会自动加载环境变量,会报java: command not found,所以需要执行一次环境变量加载--><command><![CDATA[cd ${path}/bin; pwd; sh ./stop.sh; source /etc/profile; sh ./start.sh]]></command></commands><!-- 显示运行命令的输出结果 --><displayCommandOutputs>true</displayCommandOutputs></configuration>
</plugin>

第四步,执行插件命令:

mvn wagon:upload-single wagon:sshexec -X

实践启示录

在父工程的 build 节点中统一配置 wagon 插件信息,使用 artifictId 属性针对各个子模块打包部署,很方便。

<fromFile>target/${artifactId}-${version}.jar</fromFile>
<url>scp://${serverIp}${path}/${artifactId}/lib</url>

参考

  1. 《远程 Shell 执行 Java 命令,环境变量问题》
  2. 《主机密码有特殊字符,wagon-plugin 插件的用法》
  3. 《wagon-maven-plugin 基本用法》