How do I deploy to Tomcat using Ant?
Author: Deron Eriksson
Description: This tutorial describes how to deploy to Tomcat with Ant and Tomcat's Manager.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20

Page: < 1 2 3

(Continued from page 2)

The 'url' attribute is the URL to the TomcatSW Manager application, which you are probably familiar with. The Tomcat Manager application is shown below.

Tomcat Manager

The 'username' and 'password' are the Tomcat username and password that you'd like to use to connect to the Tomcat Manager application. Note that you should have 'manager' access in Tomcat, which can be checked in your Tomcat server's tomcat-users.xml file (in the Tomcat conf directory).

Sample tomcat-users.xml File

  <role rolename="MyRole" description="My Role"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <group groupname="MyGroup" description="My Group"/>
  <user username="myuser" password="mypassword" fullName="whatever" roles="admin,manager"/>

The 'deploy' task's 'path' attribute represents the context (ie, path) that Tomcat should use to allow users to access the application. The 'deploy' task's 'war' attribute represents the path to our copy of the warW file of our project that AntSW builds.

If I double-click the 'deploy' task for tomcat-demo in Eclipse's Ant view, the war file built by the script will be deployed to the Tomcat server.

execution of 'deploy' task

In the Tomcat Manager's application list, we can see that the war file for 'tomcat-demo' has been deployed to our Tomcat server.

Tomcat Manager's application list

I'd like to quickly go over one other target, the build-and-deploy target.

	<target name="build-and-deploy" depends="war,undeploy,deploy" />

The 'build-and-deploy' target calls the 'war', 'undeploy', and 'deploy' targets. It builds the war file for tomcat-demo, tries to undeploy the tomcat-demo application from Tomcat, and then deploys the war file to the Tomcat server.

Notice that the 'undeploy' task has the following attribute:


Normally if there's an error, the build.xml execution stops. However, this says that if an error is detected, Ant should continue executing. This is important for 'build-and-deploy' to work if 'tomcat-demo' currently isn't already deployed, since if 'tomcat-demo' happens to not be deployed and we try to undeploy the non-existent application, we would receive an error.

However, if I try executing the 'build-and-deploy' target right now, I get the following result.

executing the 'build-and-deploy' target

The various Tomcat tasks offered by the catalina-ant.jar library make it quite easy to perform actions on your Tomcat server using Ant.

Page: < 1 2 3