Another Neat Tool (ANT) Part 1

Build tools have great importance in transforming source code and other input files into an executable form. For complex applications, it is very important to ensure that precisely the same build steps are carried out during each build and in order to produce consistent builds in a timely manner. Automating the whole process is also something that software developers need. Build tools provide all this.


C/C++ uses a build took called make. There are tasks and the dependencies are defined amoung the tasks. The build tasks are performed by invoking shell commands.


Apache Ant (acronym for another neat tool) is a Java-based build tool. Ant is similar to make as it also has tasks and we define dependencies between tasks. The difference is that instead of implementing build tasks using platform-specific shell commands, ANT uses cross-platform Java classes. 



Platform independence


ANT provides platform independence, which means that you can write a single build file and it will operate consistently on any Java platform. This is ANT’s greatest strength.


Build file


ANT build files are written in XML. We use XML elements that Ant understands, and new elements can be defined to extend Ant's capabilities. A build file consists of a single top-level project element. This project element contains one or more target elements. A target performs any number of operations, such as compiling a set of source files, deleting files etc. You are not supposed to write script for these operations because these operations are performed by specialized task tags. 


All the operations you require for the build process can be placed under one target element. But this will reduce flexibility. Normally you split the operations into logical steps and each logical step is placed under a target element. In this way, you may execute only the targets which are required.


A simple build file is given below. Default attribute is used with the project to indicate the default target.



<?xml version="1.0"?>
<project default="init">
<target name="init">
</target>
</project>


Adding description

Project elements can contain name and description attribute apart from default attribute. The target element can also have description and name attribute. The description is used to add some useful details which provide clarity. 

Java Code:

<?xml version="1.0"?>
<project default="init" name="XML parsing">
<description>
A simple project for XML parsing.
</description>
<!-- XML comments can also be used -->
<target name="init" description="Initialize parsing engine.">
<!-- perform initialization steps here -->
</target>
</project>


Properties Properties have a name and a value. You may say that it is like variables in a programming language. Thing to note is that Ant properties cannot be changed once they are set. What if you try to give an existing property a new value?? No error is shown and the property does retain its old value. The mechanism for setting properties in Ant looks like this:

Java Code:
<property name="sports" value="football"/>
To use or to refer to this property, use the following syntax:
Java Code: ${sports}
You are free to use a property in some other property. For example: Java Code: <property name="sports-database" value="${sports}.db"/>
Ant provides predefined properties which you may use as required. All the system properties are available. For example: ${user.home}. Apart from this, Ant defines a small set of its own properties, like ${ant.version} (version of Ant) ${basedir} (absolute path of the project's directory) You also have some properties to refer to files and directories on the filesystem. Since Ant provides plateform independence, it also takes care of the problems across different platforms with different path separator characters ( / versus \, for example). Ant provides a location attribute to mention the path in platform-neutral way. For example: Java Code: <!-- setting a property --> <property name="database-file" location="archive/databases/${sports}.db"/> <!-- property named: database-file --> It is up to user to use either back slash or forward slash. The slashes are converted to correct ones for the current platform. A good tip is to use relative path names instead of absolute ones where possible.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email