How do I transfer a file securely with Ant?
Author: Deron Eriksson
Description: This Ant tutorial shows how to transfer a war file securely to a server using pscp.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

In another tutorial, I created a target that used FTPW to transfer a warW file to a server. FTP is not encrypted. It would be nice if we could transfer files securely. One program that can do that is PSCP, which can be downloaded with several other useful utilities from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html.

I placed my putty folder at C:\putty for easy access, and I put 'C:\putty' in my System Path so that I could run the putty executables without having to specify their full paths.

Adding putty to System Path

With antSW, you can run executables using the <exec> task. You can specify arguments to exec with <arg> elements. In the example below, '-v' specifies that we want the pscp verbose option, which displays detailed information about what pscp is doing. The '-pw' option lets us explicitly specify the password that we connect to the server with, and that password is passed as the next argument.

NOTE: If you set up public/private keys and run pageant, you do not need to specify a password to use to connect to the server, since it can use the automatic key-based login for authentication. A great tutorial on how to do this can be found at http://www.howtoforge.com/ssh_key_based_logins_putty. So, if you do this, you can remove the '-pw' and '${ftp-password}' args.

The '${build-directory}/${war-file-name}' arg specifies the local war file to transfer. The '${ftp-userid}@${ftp-server}:${ftp-remotedir}/${war-file-name}' arg specifies the target, beginning with the username to use to connect to the server. This is followed by '@' and the server name or the server ip address. After that, this is followed by a ':' and the destination path and name of the war file.

pscp target

...
	<target name="pscp" depends="" description="securely upload war file to server">
		<echo message="uploading war file to server using pscp" />
		<exec executable="pscp.exe">
			<arg value="-v" />
			<arg value="-pw" />
			<arg value="${ftp-password}" />
			<arg value="${build-directory}/${war-file-name}" />
			<arg value="${ftp-userid}@${ftp-server}:${ftp-remotedir}/${war-file-name}" />
		</exec>
		<echo message="done uploading war file to server using pscp" />
	</target>
...

In build.xml, I created target to build my war file, upload it securely to the server using pscp, undeploy it from TomcatSW, deploy it to Tomcat from the uploaded location, and then mail me a message.

<target name="build-and-deploy-from-server (secure)" depends="war,pscp,undeploy,deploy-from-server,mail-build-and-deploy" />

(Continued on page 2)

Page:    1 2 >


Related Tutorials: