Checking If a Web Application Is Deployed

Problem

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

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.

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.[50] 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.

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> <condition property="is.webapp.deployed"> <and> <isset property="is.tomcat.started"/> <http url="${host}:${port}/${webapp.context.name}"/> </and> </condition> ...

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.