Tuesday, October 7, 2008

Quick Start Tutorial: Apache ANT

This is a quick introductory tutorial to the Ant build tool. It is intended for people starting out with Ant and Java development, and aims to provide enough detail to get started.

Introduction:

ANT (originally an acronym for Another Neat Tool) is a platform independent java based build tool written purely in java. Ant is particularly good at automating complicated repetitive tasks and thus is well suited for automating standardized build processes. Ant accepts instructions in the form of XML based documents thus are extensible and easy to maintain.

Installation:

* Download the binary distribution of Ant from http://ant.apache.org/.
* The binary distribution of Ant consists of the following directory layout.


* To install Ant, copy the distribution files to a particular directory.
* Set the ANT_HOME environment variable to the directory where ant is installed.
* Add ANT_HOME/lib/*.jar to classpath.
* Set JAVA_HOME environment variable to the directory where JDK is installed.
* Add the Ant’s bin directory to your path.

Basics: A Simple build process for Java project

Ant uses what it calls a build file to set out steps it will take. An Ant build file comes in the form of an XML document; all that is required is a simple text editor to edit the build files. The Ant installation comes with a JAXP-Compliant XML parser; this means that the installation of an external XML parser is not necessary.

Syntax: ant [options] [target [target2 [target3] …]]

Example #: Creating a COMPILE task for java

A simple example is shown below followed by a set of instructions indicating how to use Ant. This example will demonstrate on how-to write a simple build file for creating an output folder, compiling java source, and outputting the generated class files to the output folder.

Steps to build the compile ant task:

* Create similar to the below code into a file named build.xml. Create a directory and place the xml file into it.


(1) As Ant build files are XML files, the document begins with an XML declaration which specifies the version of XML in use.

(2) The root element of an Ant build file is the project element, it has three attributes. Out of these attributes, default is the only required attribute.

# name – denotes the name of the project.
# default – denotes the default target to be used when no target is specified.
# basedir – denotes the base directory of the project. By default, it denotes the parent directory of the build file.

(3) The property element allows the declaration of properties which are like user-definable variables available for use within the build file. The name attribute specifies the name of the property and value attribute specifies the value of the property. In this example, src property has a value “.” and output property has a value “build”.

In order to refer this property, specify the name of the property between ${ and }. In this example, the property src and output are later referred under target elements.

(4) The target element is used as a wrapper for a sequence of actions. A target has a name attribute so that it can be referenced from elsewhere, either externally from the command line or internally via the depends keyword, or through a direct call. It has a number of possible attributes out of which the name attribute is the only required attribute.

# name – denotes the target name which can be referenced from elsewhere.
# depends – denotes a comma separated list of all the targets on which this target depends. In other words, depends contains all the targets that must be executed prior to executing this target.
# if – allows adding conditional attribute to a target based on the value of the property.
# unless – converse of “if” attribute. That is, the targets content would be executed unless the property is set.
# description – provides a short description of the target.

In this example, the target “init” – initialize is used to create the output folder if it doesn’t exist through the mkdir element with the name specified by the output property defined earlier. The echo element is used to echo the message text to the current loggers and listeners which means System.out unless overridden. The task can also echo to a file.

(5) As explained in four, depends allows one to specify other targets that must be executed prior to the execution of this target. In this example, depends=”init” is used to indicate that the compile target requires that the target named init be executed prior to executing the body of compile.

The javac element, as used above, is a task, tasks are performed in the body of a target, in this case, the source directory is specified by referencing the src property and the destination directory is specified by referencing the output property. The example above causes javac to be executed, compiling all the .java files in the directory specified by the src property and placing the resultant .class files in the directory specified by the build property.

* Create a sample java source file and place it in the same directory as build.xml file.


* Type the command ant at the command line in the directory. This would create a directory called build under the test directory, compile the entire java source (BuildTest.java) under test directory and place the compiled class files (BuildTest.class) under the build directory that’s been created earlier.


* A nice feature of Ant is that by default, only those .java input files that have a more recent timestamp than their corresponding .class output files will be compiled.

A Typical Project: Build process of a J2EE web project

A typical J2EE web application is defined as a hierarchy of directories and files in a standard layout. Such a hierarchy can be accessed in its "unpacked" form, where each directory and file exists in the file system separately, or in a "packed" form known as a Web ARchive, or WAR file. The former format is more useful during development, while the latter is used when you distribute your application to be installed.

Example #: Using Ant to manage the J2EE web development and build process

This example will demonstrate on how-to write a build file for creating a J2EE web application.

Steps to build the web application task:

* Save the below code into a file named build.xml. Create a directory and place the xml file into it.


(1) These properties generally define the file and directory names (or paths) like application name, build directory, distribution directory, documentation directory, java source code location path, document files like html, jsp, js etc., external library path that affect where the build process stores its output.

(2) Instead of replying on the CLASSPATH environmental variable, Ant includes features that make it easier to dynamically construct the classpath for each compilation. Includes all jar files to classpath under the lib directory.

(3) The "all" target is a shortcut for running the "clean" target followed by the "compile" target, to force a complete recompile.

(4) The "clean" target deletes any previous "build" and "dist" directory, so that you can be ensured the application can be built from scratch.

(5) The "prepare" target is used to create the "build" destination directory, and copy the static contents of your web application to it.

(6) The "dist" target creates a binary distribution of your application in a directory structure ready to be archived in a war file. Note that this target depends on two others:
# "compile" so that the entire web will have been assembled
# "javadoc" so that the application Javadoc will have been created

(7) The "javadoc" target creates Javadoc API documentation for the Java classes included in your application. Normally, this is only required when preparing a distribution release, but is available as a separate target in case the developer wants to create Javadoc independently.

(8) The "prepare" target is used to create the "build" destination directory, and copy the static contents of the web application to it.

Core ANT Tasks - glimpse:

There are large numbers of tasks available with Ant; the following table will show some of the core tasks. Refer the links section to know more about other tasks.



Tips:

* Use properties – Ensures reusability of build files by using property element.
* Use relative file paths – Ensures projects portability.
* Comment Ant build files – Adding descriptions attribute to targets ensures better readability.
* Ant documentation – Ant’s documentation is a comprehensive and contains many useful examples.

Reference Links:

* Apache Ant Resource Homepage
  http://ant.apache.org/resources.html
* Apache Ant Manual
  http://ant.apache.org/manual/index.html
* Binary distribution of Apache Ant
  http://ant.apache.org/bindownload.cgi
* Complete list of Apache Ant Tasks
  http://ant.apache.org/manual/anttaskslist.html

No comments: