How do I use Ant to FTP a war file and deploy it to Tomcat?
Author: Deron Eriksson
Description: This Ant tutorial shows how to FTP a war file to a server and deploy it to Tomcat from the server.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page: < 1 2

(Continued from page 1)

The 'deploy-from-server' target is shown below. It displays a 'deploying server warW file' message to the console, and then it sleeps for 5 seconds. This may not be necessary, but I did it just to make sure that the undeploy target and the deploy-from-server target don't run into each other (if the undeploy isn't completed by the time the deploy-from-server starts).

The deploy task in the deploy-from-server-target looks very similar to the deploy task in the deploy target. However, notice that it contains the 'localwar' attribute. The naming is a little confusing in my opinion, but it refers to the path to the war file located on the server. After this war file is deployed, a 'done deploying' message is output to the console.

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

I created a 'build-and-deploy-from-server' target. It builds the war file and then ftp's the war file to the server. It undeploys the currently running application from TomcatSW and deploys the new war file on the server to Tomcat. After that, it emails me a message saying that the war file has been deployed.

	<target name="build-and-deploy-from-server" depends="war,ftp,undeploy,deploy-from-server,mail-build-and-deploy" />

If I execute the 'build-and-deploy-from-server' target, I see the following result in the console:

execution of the 'build-and-deploy-from-server' target

Using this technique successfully allowed me to deploy my application to Tomcat.

Page: < 1 2