Monday, February 1, 2016

How to let maven copy(scp) created jar/war package to other location

In real development, you may found your development environment is different from the running environment. For example, like my own case,  I'm working on a Hadoop/Spark related project.  So I have all my source code and development environment like Eclipse/Maven setup on my Windows laptop, but maven created jar file has to be deployed to a linux  server in the lab with Spark installed. Maunally copy maven created jar file to remote server for every run is tedious, so I need to let maven automatically do the file upload when running command 'mvn package'.

The basic idea is to use a maven plugin called 'antrun', run a ant task at maven lifecycle 'package'. Ant has scp task to copy files locally or remotely.  By utilizing this scp task from ant, you can copy the created jar/war file to anywhere, such as to a different local directory or to a remote host.

The follow demo try to copy a jar file to a remote host.  All you need is to modify maven pom.xml file, add and config this antrun plugin.

1. pom.xml

    <!-- .... other plugins ...-->
    <!-- =========================================== -->
    <!-- make sure antrun is after packaging plugins -->
    <!-- =========================================== -->
              <!-- keyfile+passphrase or  password, choose one -->
              <scp localFile="${project.basedir}/target/qos-spark-1.0.jar"
                remoteToFile="root@" verbose="true"
              <scp localFile="${project.basedir}/target/qos-spark-1.0.jar"
                remoteToFile="root@" verbose="true"
                password="mypassword" trust="true">
      <!-- libraries for scp impl      -->
      <!-- antrun doesn't use os's scp -->
    <!-- =========================================== -->
    <!--   antrun plugin end                         -->
    <!-- =========================================== -->

This looks long, but don't be panic. To use it you need to change some properties for <scp> part.

localFile is the src file, remoteToFile is the dest file. You also need to change the authentication information like where you private key file is by keyfile, if you use public/private key for authentication, or just set the password for ssh.

Also you can use antrun to copy created jar to other dir on local file system by replace remoteToFile to localToFile.

Also remember to put you antrun plugin after other package plugin to make sure the jar file is already created when reach antrun. It's safe to put it at the end of the plugin list.

Let's run 'mvn  package', the output looks like below.


2. See also


Post a Comment

Powered by Blogger.

About The Author

My Photo

Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.


Unordered List