Ant - Deploying Applications


In the previous chapter, we have learnt how to package an application and deploy it to a folder. In this example, we are going to take it even further.

We are going to deploy the web application directly to the application's server deploy folder, then we are going to add few Ant targets to start and stop the services. Let us contnue with the Hello World fax web application. This is a continuation of the previous chapter, the new components are highlighted in red

# Ant properties for building the springapp

# for Tomcat 5 use $appserver.home}/server/lib
# for Tomcat 6 use $appserver.home}/lib




<?xml version="1.0"?>

<project name="fax" basedir="." default="usage">
    <property file=""/>
    <property name="src.dir" value="src"/>
    <property name="web.dir" value="war"/>
    <property name="javadoc.dir" value="doc"/>
    <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
    <property name="name" value="fax"/>

    <path id="master-classpath">
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        <pathelement path="${build.dir}"/>
    <target name="javadoc">
      <javadoc packagenames="faxapp.*" sourcepath="${src.dir}" 
      destdir="doc" version="true" windowtitle="Fax Application">
        <doctitle><![CDATA[<h1>= Fax Application 
        <bottom><![CDATA[Copyright © 2011. All 
          Rights Reserved.]]></bottom>
        <group title="util packages" packages="faxapp.util.*"/>
        <group title="web packages" packages="faxapp.web.*"/>
        <group title="data packages" 

    <target name="usage">
        <echo message=""/>
        <echo message="${name} build file"/>
        <echo message="-----------------------------------"/>
        <echo message=""/>
        <echo message="Available targets are:"/>
        <echo message=""/>
        <echo message="deploy    --> Deploy application 
          as directory"/>
        <echo message="deploywar --> Deploy application 
          as a WAR file"/>
        <echo message=""/>

    <target name="build" description="Compile main 
      source tree java files">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" source="1.5" 
          target="1.5" debug="true"
          deprecation="false" optimize="false" failonerror="true">
            <src path="${src.dir}"/>
            <classpath refid="master-classpath"/>

    <target name="deploy" depends="build" 
      description="Deploy application">
        <copy todir="${deploy.path}/${name}" 
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>

    <target name="deploywar" depends="build" 
      description="Deploy application as a WAR file">
        <war destfile="${name}.war"
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
        <copy todir="${deploy.path}" preservelastmodified="true">
            <fileset dir=".">
                <include name="*.war"/>

    <target name="clean" description="Clean output directories">
            <fileset dir="${build.dir}">
                <include name="**/*.class"/>

    <!-- ============================================================ -->
    <!-- Tomcat tasks -->
    <!-- ============================================================ -->

    <path id="catalina-ant-classpath">
        <!-- We need the Catalina jars for Tomcat -->
        <!--  * for other app servers - check the docs -->
        <fileset dir="${appserver.lib}">
            <include name="catalina-ant.jar"/>

    <taskdef name="install" 
        <classpath refid="catalina-ant-classpath"/>
    <taskdef name="reload" 
        <classpath refid="catalina-ant-classpath"/>
    <taskdef name="list" 
        <classpath refid="catalina-ant-classpath"/>
    <taskdef name="start" 
        <classpath refid="catalina-ant-classpath"/>
    <taskdef name="stop" 
        <classpath refid="catalina-ant-classpath"/>

    <target name="reload" description="Reload application in Tomcat">
        <reload url="${tomcat.manager.url}"

In this exercise, we have used Tomcat as our application server. First, in the build properties file, we have defined some additional properties.

  • The appserver.home points to the installation path to the Tomcat application server.

  • The appserver.lib points to the library files in the Tomcat installation folder.

  • The deploy.path variable now points to the webapp folder in Tomcat.

Applications in Tomcat can be stopped and startedusing the Tomcat manager application. The URL for the manager application, username and password are also specified in the file. Next we declare a new CLASSPATH that contains the catalina-ant.jar. This jar file is required to execute Tomcat tasks through Apache Ant.

The catalina-ant.jar provides the following tasks:

InstallTaskInstalls a web application. Class Name: org.apache.catalina.ant.InstallTask
ReloadTaskReload a web application. Class Name: org.apache.catalina.ant.ReloadTask
ListTaskLists all web applications. Class Name: org.apache.catalina.ant.ListTask
StartTaskStarts a web application. Class Name: org.apache.catalina.ant.StartTask
StopTaskStops a web application. Class Name: org.apache.catalina.ant.StopTask
ReloadTask Reloads a web application without stopping. Class Name: org.apache.catalina.ant.ReloadTask

The reload task requires the following additional parameters. - 1) URL to the manager application 2)Username to restart the web application 3) Password to restart the web application 4)Name of the web application to be restarted

Let us issue the deploy-war command to copy the webapp to the Tomcat webapps folder and then let us reload the Fax Web application. The following outcome is the result of running the ant file:

C:\>ant deploy-war
Buildfile: C:\build.xml

Total time: 6.3 seconds

C:\>ant reload
Buildfile: C:\build.xml

Total time: 3.1 seconds

Once the above task is run, the web application is deployed and the web application is reloaded.