Controlling Tomcat using Ant scripting

By | May 26, 2010

As a Java webdeveloper you are faced with a lot of different aspects of development. One is testing and debugging code, to do this you need an application server like Tomcat. One of the issues I recently encountered was the need to control various Tomcat instances with a single Ant build script. Below are some of the solutions I’ve used to manipulate Tomcat.

Note: some parts of this script rely on the ant-contrib library. Download this library and include it in the tomcat.xml with the following code:
[code=xml]

[/code]

Starting tomcat

Though starting tomcat may seem easy. You could just use the development IDE (like Netbeans) to start and stop Tomcat, but what this lacks is the ability to control several instances with easy shortcuts. So I defined the following Ant macrodef in a file called tomcat.xml:

[code=xml]

 

[/code]

This macro is really easy. First it starts of by checking if tomcat is not already running (well actually it just checks to see if there is something running on port 80). If nothing is running then tomcat is started using the java defined by the ant property tomcat.java.home. This macro also depends on some other properties that are set by a different macro, which will be a bit further on in the post. Tomcat is started by using the Bootloader class provided by the server, to make sure it loads correctly the classpath is set.

The last step in the macro is to wait for tomcat to start, this has to be done since we start tomcat in a seperate java instance (the spawn option).

Stopping Tomat

Off course you also want to be able to stop tomcat to load some new data, or an entire web-app. I use the following macro to stop Tomcat:

[code=xml]

 

[/code]

Just like with the starting of Tomcat I use the provided Bootloader class to instruct tomcat to stop. Again I use the java that is set in tomcat.java.home as well as some other properties loaded by another macro. Since Tomcat sometimes fails to stop gracefully (due to poorly designed webapps) you also have to kill the Java process of Tomcat. This is done with the call to kill-java.

Killing any java process

As you saw in the tomcat-stop macro I used a macro called kill-java to make sure that Tomcat is really killed and no longer running in the background. The macro is as follows:

[code=xml]

[/code]

This macro is really simple and relies on the jps application provided by Java. This application returns a list of all running processes with name and process id (PID). All we need to do is get the line containing the process name provided and strip everything except the PID.

Please note: the task killing the proces is designed for Windows, you could change this with kill in Linux.

Rounding it up

Though you now have all you need to manipulate tomcat there is one last macro that we are tegenkant upon. That is the initializer of the various Tomcat properties. I use the following macro:

[code=xml]

[/code]

This little macro will initialize tomcat for a specific environment (for exemple DEV). Of course none of this works without the loading of some preset properties in a property file tomcat.properties. Which contains the following data:

 tomcat.java.home=C:/jdk1.5.0_22_32b

 tomcat.DEV.dir=d:/jakarta/tomcat5.0
 tomcat.DEV.server=tomcat55-DEV
 tomcat.DEV.port=8000

 tomcat.MAIN.dir=d:/jakarta/tomcat5.0-MAIN
 tomcat.MAIN.server=tomcat55-MAIN
 tomcat.MAIN.port=8000

 

Leave a Reply