Section 8.5. Deploying to Tomcat

8.5. Deploying to Tomcat

Tomcat (available from, the reference Web server for servlets and JSP, has become more attractive to Ant developers since it comes with custom Ant tasks for deployment. Copy the file server/lib/catalina-ant.jar from your Tomcat 5 installation into the lib directory of your Ant installation to use these tasks.

The Tomcat deployment tasks are deploy, reload, and undeploy; to use them, add these taskdef elements (discussed in Chapter 11) to your build file:

<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/> <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"/> <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"/>

To use these tasks, you'll need manager privileges with Tomcat; edit conf/tomcat-users.xml to add manager privileges for a username (admin here) and password like this:

<?xml version='1.0' encoding='utf-8'?> <tomcat-users>   <role rolename="manager"/>   <role rolename="role1"/>   <role rolename="tomcat"/>   <user username="admin" password="password" roles="manager"/>   <user username="role1" password="tomcat" roles="role1"/>   <user username="tomcat" password="tomcat" roles="tomcat"/>   <user username="both" password="tomcat" roles="tomcat,role1"/> </tomcat-users>

You can use the deploy task to deploy a web application to Tomcat from Ant like this:

<target name="install">     <deploy url="${manager.url}"         username="${manager.username}"         password="${manager.password}"          path="${app.path}"         localWar="file://${build}"/> </target>

Here, manager.url is the URL of the Tomcat manager servlet. The default name for this servlet is "manager", so this is something like http://localhost:8080/manager. The app.path property holds the context path at which this application should be deployed (usually / plus the name of the application as you want to use it in the URL to access the application online). The build property holds the location at which you build the Web application as it should be installed in the Tomcat webapps directory.

If you have installed an application and want Tomcat to recognize you have updated Java classes, use the reload task instead:

<target name="reload">     <reload url="${manager.url}"          username="${manager.username}"          password="${manager.password}"          path="${app.path}"/>  </target>

To remove a Web application, use the undeploy task:

<target name="remove">     <undeploy url="${manager.url}"         username="${manager.username}"         password="${manager.password}"         path="${app.path}"/> </target>

    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner © 2008-2017.
    If you may any questions please contact us: