How do I FTP a file with Ant?
Author: Deron Eriksson
Description: This tutorial describes how to use Ant to FTP a file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

In another tutorial, we used AntSW to create a warW file for a web application in EclipseSW. Typically, you build a war (web archive) file as a way to package and distribute your web application to a servletW container like TomcatSW running on a server. As a result, it would be nice to automate the process of FTPing our war file to our server using Ant. This will be demonstrated in this tutorial.

The build.xml file used in this tutorial is shown below.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="tomcat-demo" default="war" basedir=".">

	<property name="project-name" value="${ant.project.name}" />
	<property name="builder" value="TeamCakes" />
	
	<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-server" value="SERVER_GOES_HERE" />
	<property name="ftp-userid" value="USERID_GOES_HERE" />
	<property name="ftp-password" value="PASSWORD_GOES_HERE" />
	<property name="ftp-remotedir" value="uploaded-wars/${project-name}/${build-info.year-month-day}" />
	
	<target name="war" depends="">
		<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" 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>

</project>

FTPing a file using Ant requires two jarW files that don't come included by default with the version of Eclipse used in this tutorial. These libraries are Jakarta Commons NetS and Jakarta OROS. Commons Net can be downloaded at http://commons.apache.org/downloads/download_net.cgi and Jakarta ORO can be downloaded at http://jakarta.apache.org/site/downloads/downloads_oro.cgi.

I downloaded the libraries and placed the jar files in a directory in a project in my Eclipse workspace. I called the directory 'ant-jars'.

ant-jars directory

Next, I went to Windows → Preferences → Ant → Runtime. On Ant's ClasspathW tab, I selected 'Ant Home Entries' and clicked 'Add JARs'.

Ant Runtime Preferences

I selected the two jar files and clicked OK.

jar file selection

After that, the two jar files appear in the list of 'Ant Home Entries'.

Ant Home Entries

(Continued on page 2)

Page:    1 2 >