10.4 Removing a Web Application from Tomcat

10.4.1 Problem

You want to remove an old version of a web application without stopping and restarting Tomcat.

10.4.2 Solution

Tomcat provides an Ant task called RemoveTask that removes a WAR file to Tomcat using Tomcat's Manager web application.

10.4.3 Discussion

Before deploying a new version of a web application, Tomcat requires the current version of the web application be removed from the context path. This requirement is somewhat frustrating because the Ant build process must first ensure that the current web application is removed before trying to deploy; otherwise, the build process fails. Example 10-2 shows how to use Tomcat's RemoveTask, aptly named remove, to remove a web application from a given context path. A new target, undeploy, invokes the remove task. This target only executes if the property is.webapp.deployed is "true". The init target sets this property to "true" if there is a web application installed on the context path. Recipe 10.5 delves into how this is done.

Example 10-2. Removing a web application from Tomcat
<target name="undeploy" depends="init" if="is.webapp.deployed">
  <taskdef name="remove" classname="org.apache.catalina.ant.RemoveTask">
      <path location="${env.CATALINA_HOME}/server/lib/catalina-ant.jar"/>


First, create a task definition so Ant knows about the remove task. The task is found at $CATALINA_HOME/server/lib/catalina-ant.jar, where $CATALINA_HOME is the base directory for Tomcat. Next, the remove task is executed and removes the web application from Tomcat on a specific context path. The remove task defines four attributes:

  1. The url attribute specifies the location of the Manager web application. By default, Tomcat installs this on the context path /manager. For example, the URL might be http://localhost:8080/manager.

  2. The username attribute specifies the name of the user who wishes to use the Manager application.

  3. The password attribute specifies a password for authenticating the username with the Manager application.

  4. The path attribute specifies the context path of the web application to remove.

The remove task fails if there is not an existing web application installed on the current context path. Failure causes the build processes to halt. Before trying to remove the web application, check to see if it exists. Recipe 10.5 shows how to do this.

10.4.4 See Also

Recipe 10.5 shows how to determine if Tomcat is started and if a web application is deployed on a given context path.