image
好久没写文章了,都忘了自己的初衷了,同志们还是要坚持呀。。。今天就给大家介绍一款好玩的插件。
我们在项目开发过程中,经常需要部署代码到开发环境,每天可能有好多次,每次都需要mvn clean package/install,然后上传到服务器,重启容器。为解决这些繁琐的步骤,除了常用的Jenkins、TeamCity、Travis CI等比较重型的CI/DI集成工具外,我们还可以用wagon-maven-plugin这个插件来完成。
下面我们去官网看看,这个插件是什么和能干什么?
Overview
Use this plugin to view and transfer resources between repositories using Maven Wagon.
Goals Overview
The Wagon Maven Plugin has the following goals.
- wagon:upload-single uploads the specified file to a remote location.
- wagon:upload uploads the specified set of files to a remote location.
- wagon:download-single downloads the specified file from a remote location.
- wagon:download downloads the specified set of files from a remote location.
- wagon:list lists the content of a specified location in a remote repository.
- wagon:copy copies a set of files under a Wagon repository to another.
- wagon:merge-maven-repos merges , including metadata, a Maven repository to another.
- wagon:sshexec Executes a set of commands at remote SSH host
简单概括,它能在存储库之间查看和传输资源。这正是我们想要的(我们本地和服务器本质就是两个存储库)。其中wagon:upload-single、wagon:sshexec这两个goal,是我们常用的,下面我们进行项目实战。
首先添加Maven Wagon SSH
这个东东可以为Wagon插件,提供SCP和SFTP的能力。这样我们就可以发布和部署程序到服务器上。
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.8</version>
</extension>
</extensions>
配置插件,绑定package周期
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>upload-deploy</id>
<!-- 运行package打包的同时运行upload-single和sshexec -->
<phase>package</phase>
<goals>
<goal>upload-single</goal>
<goal>sshexec</goal>
</goals>
<configuration>
<!-- 需要上传的本地jar包 -->
<fromFile>target/oceanviewAdmin-1.0-SNAPSHOT.jar</fromFile>
<!-- 远程服务器的目录,必须真实存在 -->
<url>scp://user:password@url/opt</url>
<!-- sshexec 在服务器上需要执行的命令行 -->
<commands>
<!-- 杀死原来的进程 -->
<command>pkill -f test.jar</command>
<!-- 重新启动test.jar,程序的输出结果写到nohup.out文件中 -->
<command>nohup java -jar /opt/test.jar >/opt/test.out 2>&1 &</command>
</commands>
<!-- 显示运行命令的输出结果 -->
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</execution>
</executions>
</plugin>
还有其他一些参数,比如project、settings、skip等因为不常用,我们就不在一一举例。
运行 mvn package
运行完后,我们发现我们本地的程序已经自动部署到服务器上了。
完毕!欢迎小伙伴们,在评论区一起交流.