Tutorials Navigation

Tutorials: 14,133 Categories: 40
Total Tutorial Views: 26,832,089

Apache Ant Tutorial - Packaging Applications

Tutorial Name: Apache Ant Tutorial - Packaging Applications   NEW

Category: PC Miscellaneous

Submitted By: Timezone

Date Added:

Comments: 0

Views: 29


We have learnt the different aspects of Ant using the Hello World Fax web application in bits and pieces.

Now it is time to put everything together to create a full and complete build.xml file. Consider build.properties and build.xml files as follows


deploy.path = c:\tomcat6\webapps


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

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

<bottom><![CDATA[Copyright 2011. All Rights Reserved.]]>

<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}" preservelastmodified = "true">
<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"/>

In this example

We first declare the path to the webapps folder in Tomcat in the build properties file as the deploy.path variable.

We also declare the source folder for the java files in the src.dir variable.

Then we declare the source folder for the web files in the web.dir variable. javadoc.dir is the folder for storing the java documentation, and build.dir is the path for storing the build output files.

Then we declare the name of the web application, which is fax in our case.

We also define the master class path which contains the JAR files present in the WEB-INF/lib folder of the project.

We also include the class files present in the build.dir in the master class path.

The Javadoc target produces the javadoc required for the project and the usage target is used to print the common targets that are present in the build file.

The above example shows two deployment targets : deploy and deploywar.

The deploy target copies the files from the web directory to the deploy directory preserving the last modified date time stamp. This is useful when deploying to a server that supports hot deployment.

The clean target clears all the previously built files.

The deploywar target builds the war file and then copies the war file to the deploy directory of the application server.


Please take one second and rate this tutorial...

Not a Chance


"Apache Ant Tutorial - Packaging Applications" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.