Realize rapid deployment of SpringBoot project to server through Maven plugin wagon-maven-plugin

Related reference content link address:

1. Overview of wagon-maven-plugin

Maven plug-in wagon-maven-plugin to automatically complete the deployment.

When developing the springboot project, (testing) the deployment project, you must first package it into a jar file, and then use the shell command to stop the original service on the linux server uploaded by SCP, delete the original code, and then run the jar just uploaded..., this It is a series of repetitive and tedious operations. And wagon-maven-pluginyou can simplify the complicated operation.

Two, code examples

2.1 Configure Linux server username and password

<properties>
        <!--服务器项目运行的地址-->
        <service-path>/project/</service-path>
        <pack-name>${project.artifactId}-${project.version}.jar</pack-name>
        <!--ssh登录服务器的ip和端口 端口一般默认22-->
        <remote-addr>ip:port</remote-addr>
        <remote-username>服务器用户名</remote-username>
        <remote-passwd>服务器密码</remote-passwd>
</properties>

2.2 maven depends on jar

<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/wagon-maven-plugin -->
<dependency>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>wagon-maven-plugin</artifactId>
   <version>2.0.0</version>
</dependency>

2.3 Change the build of pom.xml

<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/${pack-name}</fromFile>
            <url><![CDATA[scp://${remote-username}:${remote-passwd}@${remote-addr}${service-path}]]></url>
            <!-- 在服务器执行的命令集合 -->
            <commands>
                <!-- 杀死原来的jar进程 -->
                <command>pkill -f ${pack-name}</command>
                <!-- 重新启动jar进程,程序的输出结果写到log文件中 -->
                <command><![CDATA[nohup java -jar ${service-path}/${pack-name} --spring.profiles.active=dev > ${service-path}/bd.log 2>&1 & ]]></command>
                <command><![CDATA[netstat -nptl]]></command>
                <command><![CDATA[ps -ef | grep java | grep -v grep]]></command>
            </commands>
            <!-- 显示运行命令的输出结果 -->
            <displayCommandOutputs>true</displayCommandOutputs>
        </configuration>

    </plugin>
</plugins>
</build>

2.4 execute command

Execute the following mvn command in the terminal in the same directory as the pom.xml file

mvn clean package wagon:upload-single wagon:sshexec

Three, 2, configuration

3.1 Basic configuration of wagon-maven-plugin

<build>
	<finalName>assets</finalName>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>2.10</version>
		</extension>
	</extensions>

	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>wagon-maven-plugin</artifactId>
			<version>1.0</version>			
			<configuration>				
				<!-- 需要部署的文件 -->
				<fromFile>target/assets.jar</fromFile>
				<!-- 部署目录  用户:密码@ip+部署地址:端口 -->
				<url><![CDATA[ scp://root:密码@192.168.1.100:28/usr/tomcat_assets/ ]]></url>
				
				<!--shell 执行脚本 -->
				<commands>
					<!-- 停止服务-->
					<command>sh /usr/tomcat_assets/stop.sh</command>
					<!-- 启动服务 -->
					<command>sh /usr/tomcat_assets/start.sh</command>
				</commands>
				<displayCommandOutputs>true</displayCommandOutputs>
			</configuration>
		</plugin>
	</plugins>
</build>

Steps for usage:

  1. Package first and execute mvn clean package. We are familiar with the maven packaging command, here to package the springboot project;
  2. Then upload the packaged file and execute wagon:upload-single:;
  3. Execute shell commands (scripts), mainly to stop, delete the original service, start a new service, execute: wagon:shexec.

If you want to perform with the upload files, execute shell commands, the command will merge the two to one, namely: mvn wagon:upload-single wagon:sshexec. (Note: The order must be in order)

If you want to package, upload files, execute shell commands three performed together, execute: mvn clean package wagon:upload-single wagon:sshexec.

3.2 wagon:upload-single wagon:sshexecincorporated into packagethe command (optimization, optional step)

If you think the above command complex, every time package, upload a file, execute a shell script to perform the three together, you can upload files, execute the shell script command incorporated into packaging, will soon wagon:upload-single wagon:sshexecmerge into packagecommand, when you execute the command package package, the Upload files and execute shell commands will be executed.

<build>
	<finalName>assets</finalName>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>2.10</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>						
						<!-- 需要部署的文件 -->
						<fromFile>target/assets.jar</fromFile>
						<!-- 部署目录  用户:密码@ip+部署地址:端口 -->
						<url><![CDATA[ scp://root:密码@192.168.1.100:28/usr/tomcat_assets/ ]]> </url>

						<!--shell 执行脚本 -->
						<commands>
							<!-- 停止服务-->
							<command>sh /usr/tomcat_assets/stop.sh</command>
							<!-- 启动服务 -->
							<command>sh /usr/tomcat_assets/start.sh</command>
						</commands>
						<displayCommandOutputs>true</displayCommandOutputs>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>     

Usage: execution mvn clean packagecan be.

Here's mvn clean packagethe equivalent of execution mvn clean package wagon:upload-single wagon:sshexec.

Description:

This step only optimizes the execution of packaging, uploading, and executing shell commands, and is not a necessary configuration.

3.3 The password is in the settings.xml of maven (optimized configuration)

The file upload will involve the password. The password can be placed in pom.xml (demonstrated above) or in the settings.xml of maven.

3.3.1 Configure the password in settings.xml

<servers>
	<server>  
		<id>assets</id>  
		<username>root</username>  
		<password><![CDATA[密码]]></password>  
	</server>
</servers>

Note: <id> The value must be unique.

3.3.2 Pom.xml configuration

<configuration>Label to increase <serverId>its value and the settings.xml <server>label <id>of the same value.

<configuration>
	<serverId>assets</serverId>
</configuration>

as the picture shows:
Insert picture description here

3.4 Scripts related to stopping and starting services

stop.sh Script to stop the service:

#!/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 Start script:

#!/bin/bash

fileDir=/usr/tomcat_assets
fileName=assets.jar

nohup  /usr/java/jdk1.8.0_201/bin/java -jar  ${fileDir}/${fileName} > ${fileDir}/assets.log   2>&1 &

echo $?

echo 'Start Success! '

3.5 It has nothing to do with wagon-maven-plugin, but the configuration that the project may use (optional configuration)

<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>
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
		<!-- 修改相应的SpringBootApplication.java -->
		<mainClass>xxx.yyy.zzz.Application</mainClass>
	</configuration>
	<executions>
		<execution>
			<goals>
				<goal>repackage</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Guess you like

Origin blog.csdn.net/An1090239782/article/details/112243859