Starting Tomcat with Ant

Problem

You want to start Tomcat using Ant.

Solution

Create a target that invokes the custom Ant task com.oreilly.javaxp.tomcat.tasks.StartTomcatTask.

Discussion

Typically, a server is started from a command prompt using a predefined script distributed with the server. To facilitate test-first programming, we need the ability to start Tomcat from Ant. Specifically, the Ant build process needs to start Tomcat and wait for Tomcat to become available before continuing. As of this writing, there is no generic way to solve this problem. So we created a new Ant task called StartTomcatTask to provide the functionality needed.

Example 10-4 shows the AbstractTomcatTask , which is the base class for the StartTomcatTask (Example 10-5) and StopTomcatTask (Recipe 10.7). This task extends Ant’s Task, and is directly referenced in a buildfile.

Example 10-4. AbstractTomcatTask

package com.oreilly.javaxp.tomcat.tasks; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; public abstract class AbstractTomcatTask extends Task { private TomcatSupport tomcatSupport; /** * Overrides the base class implementation to instantiate the * <code>TomcatSupport</code> class. */ public void init( ) { this.tomcatSupport = new TomcatSupport(this, getScriptToExecute(), isStarting( )); } /** * @return the name of the script to execute. For Tomcat 4.0 and * higher a valid filename might be 'startup'. The name of the script * should <strong>not</strong> include the extension. ...

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.