Coding With Fun
Home Docker Django Node.js Articles FAQ

Ant deploys the app

May 25, 2021 Apache Ant

Table of contents

Ant deploys the app

In previous chapters, we learned how to package an application and how to deploy it to a folder.

In this section, we'll deploy the web application directly to a deployment folder for the application server. A fter that, we'll add some Ant targets to start and stop the service. L et's Hello World fax fax web application. This chapter is a continuation of the previous chapter, and all new components are highlighted in bold.

# 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 = </h1>]]></doctitle>
         <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" packages="faxapp.entity.*:faxapp.dao.*"/>


   <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" webxml="${web.dir}/WEB-INF/web.xml">
         <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" classname="org.apache.catalina.ant.InstallTask">
   <classpath refid="catalina-ant-classpath"/>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
   <classpath refid="catalina-ant-classpath"/>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
   <classpath refid="catalina-ant-classpath"/>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
   <classpath refid="catalina-ant-classpath"/>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
   <classpath refid="catalina-ant-classpath"/>

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

In this example, we've used Tomcat as the server for our application. First, in the build property file, we've defined some additional properties.

  • appserver.home points to the installation path of the Tomcat server.
  • appserver.lib points to the library file under the installation file of the Tomcat server.
  • The deploy.path variable currently points to Tomcat's web application folder.

Applications in Tomcat can be started and stopped by using Tomcat management applications. T he Unified Resource Locator (URL) that manages the application, and the username and password are also specified in the folder. N ext, we declare a new CLASSPATH to include catalina-ant .jar. To run Tomcat through Apache Ant, this jar file is required.

Catalina-.jar provides the following tasks:

Property Describe
InstallTask Install a web application. The class name is: org.apache.catalina.ant.InstallTask
ReloadTask Reinstall a web application. The class name is: org.apache.catalina.ant.ReloadTask
ListTask List all web applications. Class Name: org.apache.catalina.ant.ListTask
StartTask Launch a web application. The class name is: org.apache.catalina.ant.StartTask
StopTask Stop a web application. The class name is: org.apache.catalina.ant.StopTask
ReloadTask Reload a web application that doesn't need to stop. The class name is: org.apache.catalina.ant.ReloadTask

Overloading a task requires the following additional parameters:

  • Manage the URL of the application
  • The user name of the restart web application
  • The password to restart the web application
  • The name of the rebooted web application

Let's issue a command to deploy war (deploy-war) to copy the web application into Tomcat's webapps folder. A t the same time, we reload the fax web application. The output below 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 tasks are run, the web application is deployed and reloaded.