1. 程式人生 > >Maven學習總結(48)——利用wagon-maven-plugin外掛自動化部署Jar包/war包到Linux遠端伺服器

Maven學習總結(48)——利用wagon-maven-plugin外掛自動化部署Jar包/war包到Linux遠端伺服器

前言:

Maven專案可使用mvn package指令打包,打包完成後包位於target目錄下,要想在遠端伺服器上部署,首先要將包上傳到伺服器。程式開發完成後部署到線上Linux伺服器,可能需要經歷打包、將包檔案傳到伺服器、SSH連上伺服器、敲命令啟動程式等一系列繁瑣的步驟。實際上這些步驟都可以通過Maven的一個外掛wagon-maven-plugin來自動完成。wagon-maven-plugin是maven外掛中的一種,其作用是去除我們部署時繁複的步驟,不用再手動上傳jar包或者war包到指定伺服器路徑下面。

一、在Maven的配置檔案settings.xml中配置好server的使用者名稱和密碼

<servers>
    <server>
        <id>mylinuxserver</id>
        <username>xxg</username>
        <password>123456</password>
    </server>
</servers>

二、在專案的pom.xml中配置wagon-maven-plugin外掛

1、jar包的幾個配置示例如下

 <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                  <!--這裡的id與之後的setting.xml配置項中的要一致 -->
                    <serverId>demo-test</serverId>
                  <!--本地包路徑 -->
                    <fromDir>target/</fromDir>
                  <!--上傳哪些包 -->
                    <includes>${project.artifactId}.jar</includes>
                  <!--包儲存到伺服器哪個路徑下面,注意是scp,要支援這個命令才行 -->
                    <url>scp://172.168.1.11:22/opt/micservice</url>
                  <!--伺服器目標路徑,與url配合使用 -->
                    <toDir>apps/${project.artifactId}</toDir>
                  <!--因為存在伺服器的重啟之類的操作,wagon也支援使用shell命令,可以有多個command標籤哦,根據先後順序執行-->
                    <commands>
                        <command>cd /opt/micservice/ ; ./dev restart ${project.artifactId} test</command>
                    </commands>
                  <!-- 顯示執行命令的輸出結果 -->
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>
        </plugins>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.8</version>
            </extension>
        </extensions>
    </build>
<build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>2.8</version>
		</extension>
	</extensions>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>wagon-maven-plugin</artifactId>
			<version>1.0</version>
			<configuration>
				<fromFile>target/test.jar</fromFile>
				<url>scp://user:
[email protected]
/home/xxg/Desktop</url> <commands> <!-- 殺死原來的程序 --> <command>pkill -f test.jar</command> <!-- 重新啟動test.jar,程式的輸出結果寫到nohup.out檔案中 --> <command>nohup java -jar /home/xxg/Desktop/test.jar > /home/xxg/Desktop/nohup.out 2>&amp;1 &amp;</command> </commands> <!-- 顯示執行命令的輸出結果 --> <displayCommandOutputs>true</displayCommandOutputs> </configuration> </plugin> </plugins> </build>

2、war包的幾個配置示例如下

<build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>2.8</version>
		</extension>
	</extensions>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>wagon-maven-plugin</artifactId>
			<version>1.0</version>
			<configuration>
				<fromFile>target/javawebdeploy.war</fromFile>
				<!-- 上傳到Tomcat的webapps目錄下 -->
				<url>scp://user:[email protected]/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
				<commands>
					<!-- 重啟Tomcat -->
					<command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
					<command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
					<command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
				</commands>
				<displayCommandOutputs>true</displayCommandOutputs>
			</configuration>
		</plugin>
	</plugins>
</build>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <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>
                        <serverId>mylinuxserver</serverId>
                        <fromFile>target/javawebdeploy.war</fromFile>
                        <url>scp://192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
                        <commands>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注意:<serverId>mylinuxserver</serverId>即上一步中在settings.xml中配置的service的id,<fromFile>target/zhy-pig.war</fromFile>是要上傳到伺服器的檔案,一般來說是jar或者war包,<url><![CDATA[scp://ssh-user:[email protected]:port/opt/dev/zhy-pig]]></url>配置伺服器的地址以及檔案上傳的目錄。

三、配置完成後,執行命令

mvn clean package wagon:upload-single wagon:sshexec

wagon:upload-single是上傳jar或者war包,wagon:sshexec是執行配置中的shell命令。如果不想執行上面的這麼長串命令,也可以使用execution。配置如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
	<artifactId>wagon-maven-plugin</artifactId>
    <executions>
	    <execution>
		    <id>upload-deploy</id>
		    <phase>package</phase>
		    <goals>
	            <goal>upload-single</goal>
	            <goal>sshexec</goal>
	        </goals>
	        <configuration>
		    <fromFile>target/zhy-pig.war</fromFile>
	            <url><![CDATA[scp://ssh-user:[email protected]:port/opt/dev/zhy-pig]]></url>
		    <commands>
			    <command><![CDATA[docker restart zhy-pig-dev]]></command>
		    </commands>
		    <displayCommandOutputs>true</displayCommandOutputs>
		    </configuration>
	    </execution>
    </executions>
</plugin>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <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>
                        <serverId>mylinuxserver</serverId>
                        <fromFile>target/javawebdeploy.war</fromFile>
                        <url><![CDATA[scp://ssh-user:[email protected]:port/opt/dev/zhy-pig]]></url>
                        <commands>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <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>
                        <serverId>mylinuxserver</serverId>
                        <fromFile>target/javawebdeploy.war</fromFile>
                        <url>scp://192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
                        <commands>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

以上mvn clean package 來代替 mvn clean package wagon:upload-single wagon:sshexec,當然開發時期,我覺得還是使用命令的方式執行,或者通過自己寫一個shell檔案,需要的時候執行一下即可。