原创

Maven学习总结(48)——利用wagon-maven-plugin插件自动化部署Jar包/war包到Linux远程服务器

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://zhanghaiyang.blog.csdn.net/article/details/83720874

前言:

Maven项目可使用mvn package指令打包,打包完成后包位于target目录下,要想在远程服务器上部署,首先要将包上传到服务器。程序开发完成后部署到线上Linux服务器,可能需要经历打包、将包文件传到服务器、SSH连上服务器、敲命令启动程序等一系列繁琐的步骤。实际上这些步骤都可以通过Maven的一个插件wagon-maven-plugin来自动完成。wagon-maven-plugin是maven插件中的一种,其作用是去除我们部署时繁复的步骤,不用再手动上传jar包或者war包到指定服务器路径下面。

一、在Maven的配置文件settings.xml中配置好server的用户名和密码

<servers>
    <server>
        <id>zhy-linux-server</id>
        <username>zhy</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:password@192.168.20.128/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:password@192.168.20.128/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:ssh-password@ip: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:ssh-password@ip: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:ssh-password@ip: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文件,需要的时候执行一下即可。

文章最后发布于: 2018-11-04 22:20:53
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览