Reputation: 314
I'm attempting to add a build task into a Maven pom file so that I can transfer a jar file as well as some xml files to an SFTP server (it's a VM with Tomcat installed) but I have been unable to so far.
I've had a look at a few links, but they either don't quite match the requirements, have missing steps, or don't work. I'm basically a beginner with Maven so I'm not sure if I'm doing it correctly.
The requirement is that I want to invoke a Maven build, with command line arguments for the hostname, username, and password (which I have working) which then will upload a jar file and the xml files to a VM with Tomcat running in it via SFTP (FTP doesn't work, and I don't think SCP will work as I don't have a passfile). I also don't want to mess with the main Maven settings.xml file to add the connection details in, which some of the links I found seem to be reliant upon.
I have the following profile so far, using FTP, but it doesn't work due to not using SFTP.
<profiles>
<profile>
<!-- maven antrun:run -Pdeploy -->
<id>deploy</id>
<build>
<plugins>
<plugin>
<inherited>false</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<configuration>
<target>
<!-- <loadproperties srcFile="deploy.properties" /> -->
<ftp action="send" server="${server-url}"
remotedir="/usr/share/myappserver/webapps/myapp/WEB-INF/lib"
userid="${user-id}" password="${user-password}" depends="no"
verbose="yes" binary="yes">
<fileset dir="target">
<include name="artifact.jar" />
</fileset>
</ftp>
<ftp action="send" server="${server-url}"
remotedir="/var/lib/myappserver/myapp/spring"
userid="${user-id}" password="${user-password}" depends="no"
verbose="yes" binary="yes">
<fileset dir="src/main/resource/spring">
<include name="*.xml" />
</fileset>
</ftp>
<!-- calls deploy script -->
<!-- <sshexec host="host" trust="yes" username="usr" password="pw"
command="sh /my/script.sh" /> -->
<!-- SSH -->
<taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
classpathref="maven.plugin.classpath" />
<taskdef name="ftp"
classname="org.apache.tools.ant.taskdefs.optional.net.FTP"
classpathref="maven.plugin.classpath" />
</target>
</configuration>
<dependencies>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-commons-net</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>jsch</groupId>
<artifactId>jsch</artifactId>
<version>0.1.29</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I tried swapping ftp for sftp, and using org.apache.tools.ant.taskdefs.optional.ssh.SFTP instead, but it didn't seem to work.
So to summarize, the SFTP config needs to be self contained within the pom file as much as possible, with stuff like the host url, username and password being passed in when invoked.
Upvotes: 1
Views: 3953
Reputation: 314
I was eventually able to get this working using PuTTy pcsp
<PUTTY_DIRECTORY>/pscp.exe" -l <USERNAME> -pw <PASSWORD> ./fileToTransfer.file <PUTTY_SESSION_ID>:/destination-directory/fileToTransfer.file
I could also run scripts using plink
<PUTTY_DIRECTORY>/plink.exe" <PUTTY_SESSION> -l <USERNAME> -pw <PASSWORD> -m ./myScriptToRunRemotely.sh
It wasn't the cleanest of solutions, but it still allows me to use it on Jenkins for deployment testing purposes.
Upvotes: 0
Reputation: 14792
I'd use the Wagon Maven plugin with its goals upload-single
and upload
instead of antrun
:
wagon:upload-single uploads the specified file to a remote location.
wagon:upload uploads the specified set of files to a remote location.
Upvotes: 1