Coding With Fun
Home Docker Django Node.js Articles FAQ

Ant encapsulation applications


May 25, 2021 Apache Ant


Table of contents


Ant encapsulation applications

With the Hello World Fax web app, we've learned a little about Ant's different aspects.

It's time to apply everything we've learned to create a comprehensive and complete .xml build file. Consider the build.properties and build .xml file:

build.properties

eploy.path = c:\tomcat6\webapps

build.xml

<?xml version = "1.0"?>

<project name = "fax" basedir = "." default = "usage">

   <property file = "build.properties"/>
   <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"/>
      </fileset>
      <pathelement path = "${build.dir}"/>
   </path>

   <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.*"/>
      </javadoc>
   </target>

   <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>

   <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"/>

      </javac>
   </target>

   <target name = "deploy" depends = "build" description = "Deploy application">
      <copy todir = "${deploy.path}/${name}" preservelastmodified = "true">

         <fileset dir = "${web.dir}">
            <include name = "**/*.*"/>
         </fileset>

      </copy>
   </target>

   <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 = "**/*.*"/>
         </fileset>
      </war>

      <copy todir = "${deploy.path}" preservelastmodified = "true">
         <fileset dir = ".">
            <include name = "*.war"/>
         </fileset>
      </copy>

   </target>

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

</project>

In the example given above:

  • We first declared the path to the webapp folder where Tomcat is stored in the build.properties file and saved it with the variable deploy.path.
  • We declare a source folder to hold the java file and save it with the variable src.dir.
  • Next, we declare another source folder to hold the web file and save it with the variable web.dir. The variable javadoc.dir is used to store java documents, and the variable build.dir is used to store the path to the configuration output file.
  • Then we name the web app, fax fax.
  • We have also defined the basic class path that contains the JAR file, which is given above in the project name: WEB-INF/lib folder.
  • We also store the class files in build.dir under the base class path.
  • This Javadoc target produces the documentation required for the project, as well as the javadoc documentation that describes the target.

The above example shows us two deployment goals: deploy and deploywar.

This deploy target copies the file from the web directory to the deployment directory and saves the last modified date timestamp. T his is useful, especially if we deploy the project to a server that supports hot deployments. ( explanation: A so-called hot deployment is an upgrade of the software while the app is running without the need to restart the app.)

This clean goal is clear to all previous build files.

This deploywar target builds the war file and then copies the war file to the deployment directory of the application server.