Maven插件之wagon-maven-plugin,远程自动化部署

wagon-maven-plugin插件可以帮助我们scp上传jar包并且远程执行shell命令

1. 配置

    <profiles>
        <profile>
            <!-- 开发环境 -->
            <id>dev</id>
            <properties>
                <user>root</user>
                <password>1234567890</password>
                <ip>192.168.122.22</ip>
                <active>dev</active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

    <build>
        <!-- maven扩展 提供ssh远程服务 是wagon-maven-plugin插件所依赖 -->
        <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>2.0.2</version>
                <configuration>
                    <!-- 拷贝目录下(执行目录) target目录下的jar包 -->
                    <fromFile>target/${project.artifactId}-${project.version}.jar</fromFile>
                    <!-- 使用scp传输文件 指定服务端 用户名密码 ip 并指定目标文件夹-->
                    <url>scp://${user}:${password}@${ip}/usr/springboot/app</url>
                    <!-- 命令列表 可以在传输完成后执行 -->
                    <commands>
                        <command>sh /usr/springboot/app/start-jar.sh restart</command>
                    </commands>
                    <!-- 显示运行命令的输出结果 -->
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>

			<!-- 以下插件和wagon-maven-plugin无关,可选配置 -->

            <!--不编译test-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

        </plugins>
    </build>

2. 使用步骤

先打包,执行mvn clean package

再上传打包的文件,执行:wagon:upload-single

执行shell 命令(脚本),主要是停止、删除原来的服务,启动新的服务,执行:wagon:shexec

或者使用IDEA,新增命令clean package wagon:upload-single wagon:sshexec


或者修改一下pom

只需要执行mvn clean package
此处的 mvn clean package 相当于执行mvn clean package wagon:upload-single wagon:sshexec

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>wagon-maven-plugin</artifactId>
    <version>2.0.2</version>
	<executions>
		<execution>				
			<id>upload-deploy</id>					
			<!-- 运行package打包的同时运行upload-single和sshexec -->
			<phase>package</phase>
			<goals>
				<goal>upload-single</goal>
				<goal>sshexec</goal>
			</goals>
				<configuration>
                   <!-- 拷贝目录下(执行目录) target目录下的jar包 -->
                   <fromFile>target/${project.artifactId}-${project.version}.jar</fromFile>
                   <!-- 使用scp传输文件 指定服务端 用户名密码 ip 并指定目标文件夹-->
                   <url>scp://${user}:${password}@${ip}/usr/springboot/app</url>
                   <!-- 命令列表 可以在传输完成后执行 -->
                   <commands>
                       <command>sh /usr/springboot/app/start-jar.sh restart</command>
                   </commands>
                   <!-- 显示运行命令的输出结果 -->
                   <displayCommandOutputs>true</displayCommandOutputs>
              </configuration>
		</execution>
	</executions>
</plugin>

3. 其他

shell脚本

stop.sh 停止服务的脚本:

#!/bin/bash

APP_NAME='assets.jar'

tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ];then
    echo 'Stop Process...'
    kill -9 $tpid
fi

sleep 1

tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`

if [ ${tpid} ];then
    echo 'Kill Process!'
    kill -9 $tpid
else
    echo 'Stop Success!'
fi

start.sh 启动脚本:

#!/bin/bash

fileDir=/usr/springboot/app
fileName=assets.jar

nohup java -jar  ${fileDir}/${fileName} > ${fileDir}/assets.log   2>&1 &

echo $?

echo 'Start Success! '
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值