News Ticker

Compile, deploy and start Tomcat with Ant script


Apache ant is a tool used to automate repetitive development tasks. One task that is particularly useful is the compile and deploy a web application. However, the process can be automated a little further by starting the Tomcat server after the final deploy task has finished.

Example Script

This example shows how to automate the starting of the Tomcat server. I use Apache Tomcat 7.0.27 ( and Ant 1.8.4 (

The assumptions are that the penultimate task is called war and that there is a file that defines the required directories.

The Build Properties

Here is my file:


Define The Class Path

Firstly you must tell Ant where the Tomcat classpath is located. We use the path task as follows:

<path id="tomcat.class.path">
    <fileset dir="${server.location}/${}/lib">
        <include name="**/*.jar"/>
        <include name="**/*.zip"/>
    <pathelement location="${server.location}/${}
    <pathelement location="${server.location}/${}

Tomcat Stop and Start Targets

We must then define the start and stop Tomcat targets as follows:

<target name="tomcat-start">
    <echo>Stop Tomcat</echo>
    <java classname="org.apache.catalina.startup.Bootstrap" fork="true"
        <jvmarg value="-Dcatalina.home=${server.location}/${}"/>
<target name="tomcat-stop" depends="tomcat-check-status" if="tomcat.started">
    <echo>Start Tomcat</echo>
    <java classname="org.apache.catalina.startup.Bootstrap" fork="true"
        <jvmarg value="-Dcatalina.home=${server.location}/${}"/>
        <arg line="stop"/>
    <sleep seconds="5"/>
<target name="tomcat-check-status">
    <condition property="tomcat.started">
        <socket server="localhost" port="8000"/>

Check Tomcat Status

The final target called “tomcat-check-status” is called by the “tomcat-stop” and Tomcat is only stopped if the target returns that Tomcat is running.

Deploy Task

The last target we need to write is “deploy”. This target is dependent on the “war” target. A call to “tomcat-stop” is made before the application is deployed to the server. After the deployment, a call to “tomcat-start” is made and the server is started.

<target name="deploy" depends="war">
        <antcall target="tomcat-stop"/>
            <echo>Deploy to server</echo>
            <copy todir="${server.location}/${}/${server.webapp}/
                <fileset dir="${build.dir}"/>
        <antcall target="tomcat-start"/>

This is a very useful addition to the normal ant tasks that deploy web applications.

Interesting Links

If you want to learn more about the power of Apache Ant then consider the following books:

Related Articles

One of the most important developments in Java EE 8 and Java 9 will be support for HTTP/2. Tomcat 9 supports HTTP/2 but it must be configured to use TLS. Head on over to Configure Tomcat 9 for HTTP/2 to find out how to add the appropriate configurations.

Apache Ant can be used to automate the compilation, deployment, and launch of Tomcat all with a simple Ant script. Find out how to automate these task in my article: Compile, deploy and start Tomcat with Ant script.

Amazon Web Services offers free usage for 12 months that allows you to install Tomcat. The free tier consists of 14 services, the EC2 service is of most interest to developers. I show you how to install and configure Tomcat on an EC2 instance: Amazon Free Usage Tier: Installing Tomcat 7 on an EC2 Linux instance.

One of the most important configuration files in Tomcat is server.xml.I have posted an example of this file in my blog post: Tomcat server.xml example.

Tomcat is often set up with Apache and mod_jk cluster. In this article I will go through a common set-up for a small production environment: a single tier, load balanced application server cluster.

I often post about Ant and Tomcat on Twitter. Follow me here

2 Trackbacks / Pingbacks

  1. Welcome « alex.theedom
  2. Apache Ant: Staring Tomcat from an Ant target « alex.theedom

Leave a Reply

%d bloggers like this: