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

(Continued from page 1)

I can run the 'build-and-deploy-from-server (secure)' target in EclipseSW by clicking on it in the AntSW view.

Execution of the 'build-and-deploy-from-server (secure)' target

When I run the target, I see the following result in the console.

Buildfile: C:\projects\workspace\java-tutorials\build.xml
war:
   [delete] Deleting: C:\projects\workspace\java-tutorials\build\java-tutorials.war
      [war] Building war: C:\projects\workspace\java-tutorials\build\java-tutorials.war
pscp:
     [echo] uploading war file to server using pscp
     [exec] Server version: SSH-2.0-OpenSSH_3.9p1
     [exec] We claim version: SSH-2.0-PuTTY_Release_0.58
     [exec] Using SSH protocol version 2
     [exec] Doing Diffie-Hellman group exchange
     [exec] Doing Diffie-Hellman key exchange
     [exec] Host key fingerprint is:
     [exec] ssh-rsa 1024 --:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--
     [exec] Initialised AES-256 client->server encryption
     [exec] Initialised HMAC-SHA1 client->server MAC algorithm
     [exec] Initialised AES-256 server->client encryption
     [exec] Initialised HMAC-SHA1 server->client MAC algorithm
     [exec] Using username "myname".
     [exec] Sent password
     [exec] Access granted
     [exec] Opened channel for session
     [exec] Started a shell/command
     [exec] Using SFTP
     [exec] Connected to 192.168.1.2
     [exec] Sending file java-tutorials.war, size=8453407
     [exec] Sent EOF message
     [exec] Server sent command exit status 0
     [exec] All channels closed. Disconnecting
     [exec] Server closed network connection
     [exec] java-tutorials.war        |          4 kB |   4.0 kB/s | ETA: 00:34:22 |   0%
            java-tutorials.war        |         40 kB |  40.0 kB/s | ETA: 00:03:25 |   0%
...
            java-tutorials.war        |       8220 kB |  38.6 kB/s | ETA: 00:00:00 |  99%
            java-tutorials.war        |       8255 kB |  38.8 kB/s | ETA: 00:00:00 | 100%
     [echo] done uploading war file to server using pscp
undeploy:
 [undeploy] OK - Undeployed application at context path /java-tutorials
deploy-from-server:
     [echo] deploying server war file
   [deploy] OK - Deployed application at context path /java-tutorials
     [echo] done deploying
mail-build-and-deploy:
     [mail] Sending email: java-tutorials.war was uploaded to the server and deployed
     [mail] Sent email with 0 attachments
build-and-deploy-from-server (secure):
BUILD SUCCESSFUL
Total time: 3 minutes 45 seconds

The Ant script above ran successfully.

The complete build.xml file that I used in this example is shown below.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="java-tutorials" default="war" basedir=".">
	<property file="build.properties" />

	<property name="war-file-name" value="${project-name}.war" />
	<property name="source-directory" value="src" />
	<property name="classes-directory" value="bin" />
	<property name="web-directory" value="web" />
	<property name="web-xml-file" value="web/WEB-INF/web.xml" />
	<tstamp prefix="build-info">
		<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
		<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
		<format property="year-month-day" pattern="yyyy-MM-dd" locale="en" />
	</tstamp>
	<property name="build-directory" value="build" />

	<property name="ftp-remotedir" value="uploaded-wars" />

	<taskdef name="start" classname="org.apache.catalina.ant.StartTask" />
	<taskdef name="stop" classname="org.apache.catalina.ant.StopTask" />
	<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
	<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />

	<target name="stop" description="stop application in tomcat">
		<stop 
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}"
		/>
	</target>

	<target name="start" description="start application in tomcat">
		<start 
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}"
		/>
	</target>
	
	<target name="undeploy" description="undeploy from tomcat">
		<undeploy 
			failonerror="no"
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}"
		/>
	</target>

	<target name="deploy" description="deploy to tomcat">
		<echo>deploying from client</echo>
		<deploy 
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}"
			war="file:/projects/workspace/${project-name}/${build-directory}/${war-file-name}"
		/>
	</target>
	
	<target name="deploy-from-server">
		<echo>deploying server war file</echo>
		<sleep seconds="5"/>
		<deploy 
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}" 
			localwar="file:/${server-home-directory}/uploaded-wars/${war-file-name}"
		/>
		<echo>done deploying</echo>
	</target>
	
	<target name="war">
		<mkdir dir="${build-directory}" />
		<delete file="${build-directory}/${war-file-name}" />
		<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
			<classes dir="${classes-directory}" />
			<fileset dir="${web-directory}">
				<!-- Need to exclude it since webxml is an attribute of the war tag above -->
				<exclude name="WEB-INF/web.xml" />
			</fileset>
			<manifest>
				<attribute name="Built-By" value="${builder}" />
				<attribute name="Built-On" value="${build-info.current-date}" />
				<attribute name="Built-At" value="${build-info.current-time}" />
			</manifest>
		</war>
	</target>

	<target name="ftp" depends="" description="upload war file to server">
		<ftp 
			server="${ftp-server}" remotedir="${ftp-remotedir}"
			userid="${ftp-userid}" password="${ftp-password}"
			action="mkdir" verbose="yes">
		</ftp>
		<ftp 
			server="${ftp-server}" remotedir="${ftp-remotedir}"
			userid="${ftp-userid}" password="${ftp-password}"
			action="send" verbose="yes" depends="yes">
			<fileset file="${build-directory}/${war-file-name}" />
		</ftp>
	</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>
	
	<target name="mail-build-and-deploy">
		<mail from="${email-from}"
		      tolist="${email-to}"
		      subject="${war-file-name} was uploaded to the server and deployed"
		      message="The ${war-file-name} file was uploaded to ${ftp-server}
in ${ftp-remotedir} and deployed to the /${project-name} context"/>
	</target>

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

The build.properties file referenced by the build.xml file is shown below.

build.properties

#build.properties file
project-name=java-tutorials
builder=TeamCakes
ftp-server=---
ftp-userid=---
ftp-password=---
tomcat-manager-url=http://---/manager
tomcat-manager-username=---
tomcat-manager-password=---
server-home-directory=---
email-to=---
email-from=---
Page: < 1 2


Related Tutorials: