Removing a Web Application from Tomcat

Problem

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

Solution

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

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">
    <classpath>
      <path location="${env.CATALINA_HOME}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

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

First, create a task definition so Ant knows about the remove task. ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.