Skip to content

Instantly share code, notes, and snippets.

@soumik-dutta
Created May 16, 2019 11:29
Show Gist options
  • Save soumik-dutta/6214e8195267a39500fb802704b75032 to your computer and use it in GitHub Desktop.
Save soumik-dutta/6214e8195267a39500fb802704b75032 to your computer and use it in GitHub Desktop.
Deploying Java app using maven plugin

Deploying Java app using maven plugin

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>
SCP the executable jar/war to the remote machine

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="">
SSH and run some command/script

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

@koma1
Copy link

koma1 commented Nov 18, 2022

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment