Objective is to deploy a simple java app to the remote machine using maven plugin. There are two sections of this plugin
- SCP the executable jar/war to the remote machine
- SSH and run some command/script
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>scp-to-remote</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- keyfile+passphrase or password, choose one -->
<!--
<scp localFile="${project.basedir}/target/qos-spark-1.0.jar"
remoteToFile="root@ip_addr:/usr/sanss" verbose="true"
keyfile="C:\Users\shengw\.ssh\ip_addr\id_rsa"
passphrase="">
</scp>
-->
<scp localFile="${project.basedir}/target/${project.name}.jar"
remoteToFile="root@ip_addr:/root/jars/${project.name}.jar"
verbose="true"
password="your_pass"
trust="true" />
<!-- calls deploy script -->
<sshexec host="ip_addr"
trust="yes"
username="root"
password="your_pass"
command="sudo systemctl restart ${project.name}" />
<!-- SSH -->
<taskdef name="sshexec"
classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
classpathref="maven.plugin.classpath" />
</target>
</configuration>
</execution>
</executions>
<!-- libraries for scp impl -->
<!-- antrun doesn't use os's scp -->
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
</plugin>
The idea is to copy the executable from your machine to the remote machine's specific folder that is mentioned
<scp localFile="${project.basedir}/target/${project.name}.jar"
remoteToFile="root@ip_addr:/root/jars/${project.name}.jar"
verbose="true"
password="your_pass"
trust="true" />
OR
<scp localFile="${project.basedir}/target/${project.name}.jar"
remoteToFile="root@ip_addr:/root/jars/${project.name}.jar" verbose="true"
keyfile="location to your .pem file or .ssh folder"
passphrase="">
We will run some command in the target machine after the trasfer is done . In this case I will restart my app server. Using the systemctl command.
<!-- calls deploy script -->
<sshexec host="ip_addr"
trust="yes"
username="root"
password="your_pass"
command="sudo systemctl restart ${project.name}" />
To know how to create a systemctl service
Deploy via SSH works fine for me
But how i can separate deploy, package, and service startup?
I need to use "package" stage for create the .WAR file only
And then, if i need to deploy it via ssh, use "mvn clean package deploy remote-start"
if i changing PHASE from PACKAGE to DEPLOY in .POM that doesn't work...