Use the Maven plugin wagon-maven-plugin to automate deployment

In the process of project development, it is often necessary to deploy code to the development environment. There may be many times a day, and mvn clean install is required each time, and then uploaded to the server. In fact, these tedious steps can be automatically completed by a Maven plug-in wagon-maven-plugin

Configure the username and password of the Linux server
In order for the wagon-maven-plugin plug-in to connect to the Linux server through SSH, you first need to configure the username and password of the server in the project pom.xml.

<properties>
		<!--wagon plugin 配置-->
		<service-path>/work/renren</service-path>
		<pack-name>${project.artifactId}-${project.version}.jar</pack-name>
		<remote-addr>192.168.1.1:22</remote-addr>
		<remote-username>root</remote-username>
		<remote-passwd>123456</remote-passwd>
	</properties>

Upload the file to the server and restart the
Maven project. You can use the mvn package command to package. After the package is completed, the package is located in the target directory. If you want to deploy on a remote server, you must first upload the package to the server. Configure the wagon-maven-plugin plugin in the pom.xml of the project:

<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>
						<!-- Kill Old Process -->
						<command>kill -9 `ps -ef |grep ${project.artifactId}.jar|grep -v "grep" |awk '{print $2}'`</command>
						<!-- Restart jar package,write result into renren.log -->
						<command><![CDATA[nohup java -jar ${service-path}/${pack-name} --spring.profiles.active=test > ${service-path}/test.log 2>&1 & ]]></command>
						<command><![CDATA[netstat -nptl]]></command>
						<command><![CDATA[ps -ef | grep java | grep -v grep]]></command>
					</commands>
					<!-- 运行命令 mvn clean package wagon:upload-single wagon:sshexec-->
					<displayCommandOutputs>true</displayCommandOutputs>
				</configuration>
			</plugin>
配置完成后,运行命令:mvn clean package wagon:upload-single wagon:sshexec

Guess you like

Origin blog.csdn.net/qq_37980436/article/details/105812671