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

(Continued from page 1)

Now, let's examine the ftpW target in build.xml. The ftp target has two tasks. The first task performs a 'mkdir' action, which creates the 'remotedir' location on the ftp server if it doesn't exist already. It connects to the server, using the userid and password that are supplied as AntSW properties. If they don't exist, this task creates an 'uploaded-wars' directory, and within that a directory consisting of the project name, and within that a directory using today's date. The verbose attribute basically instructs Ant to tell us what it is doing.

...
	<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>
...

The second ftp task performs the 'send' action. Once again, it connects to the same server with the same userid and password. It uploads our project's warW file (created by the 'war' target) to the remotedir that mkdir created. The war file is specified using the fileset tag with the file attribute.

The ftp target appears in the Ant view in EclipseSW. We can run it by double-clicking on it.

Eclipse Ant View

The execution of the ftp target is shown below.

Execution of ftp target

As you can see, Ant makes it very easy to build a war file and to upload it to a server. We can now build the war file by double-clicking the war target, and we can upload the generated war file by double-clicking on the ftp target.

If we wanted to, we could have the ftp target depend on the war target. If we do this, double-clicking the ftp target will run the war target to build the war file and then upload the war file to the server.

	<target name="ftp" depends="war" description="upload war file to server">

The execution of the ftp target with "war" in the depends is shown below.

Execution of ftp target with war in the depends
Page: < 1 2