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
<build> <plugins> <!-- .... other plugins ...--> <!-- =========================================== --> <!-- make sure antrun is after packaging plugins --> <!-- =========================================== --> <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@192.168.203.156:/usr/sanss" verbose="true" keyfile="C:\Users\shengw\.ssh\192.168.203.156\id_rsa" passphrase=""> </scp> --> <scp localFile="${project.basedir}/target/qos-spark-1.0.jar" remoteToFile="root@192.168.203.156:/usr/sanss" verbose="true" password="mypassword" trust="true"> </scp> </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> <!-- =========================================== --> <!-- antrun plugin end --> <!-- =========================================== --> </plugins> </build>
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.
How can I scp it in an exploded form.
ReplyDeleteHow do I make this run only for a specified maven profile please?
ReplyDeleteThank you!
ReplyDelete