My Windows Ant/Tomcat web application build process

I don't like the off-the-shelf process of developing web applications with Ant and Tomcat on Windows computers. Rather than get into my angst, here's the way I think the Ant build process should work with Tomcat:

  • You build your web application with Ant.
  • If the build process succeeds Ant cleans out your Tomcat directory structure -- including the old application directory and all log files -- and then automatically starts the Tomcat application server.

This doesn't happen with Ant and Tomcat off the shelf -- you always have to do things manually, whether you're using Eclipse or working from the command line.

To solve this problem I've created my own Ant tasks and batch files that I use on Windows computers. Now, whenever I want to build my web application, deploy it, clean out my Tomcat folders, and start the Tomcat server, I just issue this command from my build directory:

ant t-deploy

The code shown below from my Ant build script (build.xml) shows how this works:

<!-- ====== IMPROVING TOMCAT STARTUP PROCESS ===== -->
<property name="tomcat.home" value="${env.TOMCAT_HOME}" />
<property name="webapp.name" value="${jsp.dir.name}" />

<target name="t-deploy" 
        depends="install, t-start" 
        description="FULL DEPLOY OF THE WEB APP">
  <echo>=== BUILDING APP, CLEANING AND STARTING TOMCAT ===</echo>
</target>
 
<target name="t-start" depends="delete-logs, delete-deploy-dir">
  <echo>=== STARTING TOMCAT ===</echo>
  <sleep milliseconds="250"/>
  <exec executable="cmd">
    <arg value="/c"/>
    <arg value="${tomcat_start_script}"/>
  </exec>
</target>

<target name="delete-logs">
  <echo>=== DELETING LOG FILES ===</echo>
  <delete>
    <fileset dir="${tomcat.home}/logs/" includes="*.log"/>
  </delete>
</target>

<target name="delete-deploy-dir">
  <echo>=== REMOVING OLD DEPLOY DIRECTORY ===</echo>
  <delete dir="${war_deployment_dir}/${webapp.name}" />
</target>

Discussion

As you can see, the t-deploy task first runs the install task, then runs the t-start task. The t-start task depends on two other tasks, delete-logs, and delete-deploy-dir. The logs directory is something like C:\tomcat-6.0.16\logs, and the deploy directory is something like C:\tomcat-6.0.16\webapps\MyWebApplication.

This Ant/Tomcat integration approach depends on a Windows batch script which I named tomcat-start.bat and I keep in the same directory as my build.xml file. Here's the contents of that script:

REM
REM A special script to help startup tomcat from within 
REM the Ant build process.
REM

SET TOMCAT_HOME=C:\tomcat-6.0.16
SET CATALINA_HOME=C:\tomcat-6.0.16

C:/tomcat-6.0.16/bin/startup.bat