Sunday, October 19, 2008

ANT Scripting Making Simple

I am starting my blog with the very interesting build making Tool known as ANT (Another Neat Tool). My aim would be to make a very simple implementation of the ANT tool, so that one can grasp in a wink.

ANT is basically a java-based tool designed to be cross-platform,easy to use, extensible and scalable.It can be used in a small personal project or a large multiteam projects.

ANT is written in a XML file which has its own parser, that understand the syntax and go ahead with the task crypted in the concerned XML. Each and every Java project needs to have a build.xml file, which defines the task to be performed for the project. Its not mandatory to have the name as build.xml, so it can be user specific.

Basically, I have implemented the ANT in Netbeans IDE 6.1, but it will be better to start off in the terminal prompt rather than using any specific IDE.

Now to build a project using ANT tool you need to have ANT installed in your system .Now how to install a ANT can be obtained from the link

Here I will Create a Java class and put it to its desired destination and clean off the unnecessary directories involving in its development .So the steps goes like this :

1) Write a Java Program.

package javaapplication2;

* @author samrat
public class Main {

* @param args the command line arguments
public static void main(String[] args) {
// TODO code application logic here
System.out.println("ANT building in Process");


2) Write a Build.xml

<project name="JavaApp" default="init" basedir=".">
<description>Builds, tests, and runs the project JavaApplication2.</description>

<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />

<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes"/>

<target name="archive" depends="compile" >
<jar destfile="dist/project.jar" basedir="build/classes" />

<target name="clean" depends="archive" >
<delete dir="build" />


Below is the explanation of the build.xml file.
  • Creating required directories: Here is the simple xml which is with project name as "JavaApp". Each Project has number of task and over only init task is explained, which creates two directories build/classes and dist. The above xml file can be executed with the promt ant, so it will run the default task as mentioned in the project i.e. init. Individual task can be excetued with the following command ant <taskname>
  • Compile the Source File : Here the task compile depends upon the init task and it will only get executed if only the init is performed successfully. The java is compiled and put into "build/classes" path.
  • Archive the Class :The compiled class is archived into project.jar at dist directory.
  • Delete the build Directory : The Target clean clears the build directory.

This is a very simple approach of building a Java project using ANT build tool. I will get back to you with some more interesting applications, once I encounter it.