10.5 Checking If a Web Application Is Deployed

10.5.1 Problem

You need to check if a web application is installed on a particular context path.

10.5.2 Solution

Create an Ant target that sets a property if a given context path contains an installed web application. This target should execute before trying to remove the web application.

10.5.3 Discussion

Tomcat's Manager application fails when trying to remove a web application that does not exist. It's frustrating because the Ant build process must check if the web application is installed before attempting to remove it. Luckily, the solution is simple. Example 10-3 shows an init target that checks if the web application is installed on a given context path. The init target sets two properties: is.tomcat.started and is.webapp.deployed. The condition task sets is.tomcat.started to "true" if the nested subtask http returns a valid HTTP response code.[1] The condition task sets is.webapp.deployed to "true" if Tomcat is started and the nested http subtask returns a valid response code. A valid response code means that some type of success occurred when opening a connection to the URL. If a failure occurs, the URL is assumed to be invalid.

[1] Response codes 400 and above represent errors according to the HTTP 1.1 specification. This specification is available in RFC 2616 at http://www.ietf.org/rfc/rfc2616.txt. Response codes below 400 indicate some sort of success.

Example 10-3. Checking if a web application exists on a given context path
<target name="init">
  <condition property="is.tomcat.started">
    <http url="${host}:${port}"/>

  <condition property="is.webapp.deployed">
      <isset property="is.tomcat.started"/>
      <http url="${host}:${port}/${webapp.context.name}"/>

10.5.4 See Also

Recipe 10.4 discusses how to use Tomcat's Manager application to remove a web application from a context path.