好久沒(méi)寫文章了宦言,都忘了自己的初衷了,同志們還是要堅(jiān)持呀商模。奠旺。。今天就給大家介紹一款好玩的插件施流。
我們?cè)陧?xiàng)目開(kāi)發(fā)過(guò)程中响疚,經(jīng)常需要部署代碼到開(kāi)發(fā)環(huán)境,每天可能有好多次嫂沉,每次都需要mvn clean package/install稽寒,然后上傳到服務(wù)器,重啟容器趟章。為解決這些繁瑣的步驟,除了常用的Jenkins慎王、TeamCity蚓土、Travis CI等比較重型的CI/DI集成工具外,我們還可以用wagon-maven-plugin這個(gè)插件來(lái)完成赖淤。
下面我們?nèi)ス倬W(wǎng)看看蜀漆,這個(gè)插件是什么和能干什么?
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
簡(jiǎn)單概括咱旱,它能在存儲(chǔ)庫(kù)之間查看和傳輸資源确丢。這正是我們想要的(我們本地和服務(wù)器本質(zhì)就是兩個(gè)存儲(chǔ)庫(kù))。其中wagon:upload-single吐限、wagon:sshexec這兩個(gè)goal鲜侥,是我們常用的,下面我們進(jìn)行項(xiàng)目實(shí)戰(zhàn)诸典。
首先添加Maven Wagon SSH
這個(gè)東東可以為Wagon插件描函,提供SCP和SFTP的能力。這樣我們就可以發(fā)布和部署程序到服務(wù)器上。
<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>
<!-- 運(yùn)行package打包的同時(shí)運(yùn)行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>
<!-- 遠(yuǎn)程服務(wù)器的目錄胆数,必須真實(shí)存在 -->
<url>scp://user:password@url/opt</url>
<!-- sshexec 在服務(wù)器上需要執(zhí)行的命令行 -->
<commands>
<!-- 殺死原來(lái)的進(jìn)程 -->
<command>pkill -f test.jar</command>
<!-- 重新啟動(dòng)test.jar,程序的輸出結(jié)果寫到nohup.out文件中 -->
<command>nohup java -jar /opt/test.jar >/opt/test.out 2>&1 &</command>
</commands>
<!-- 顯示運(yùn)行命令的輸出結(jié)果 -->
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</execution>
</executions>
</plugin>
還有其他一些參數(shù)互墓,比如project必尼、settings、skip等因?yàn)椴怀S么勰欤覀兙筒辉谝灰慌e例胰伍。
運(yùn)行 mvn package
運(yùn)行完后,我們發(fā)現(xiàn)我們本地的程序已經(jīng)自動(dòng)部署到服務(wù)器上了酸休。
完畢骂租!歡迎小伙伴們,在評(píng)論區(qū)一起交流.