Saturday, March 9, 2013

All About Ant...


I felt like doing a small write up ANT build tool..
ANT - acronym for "Another Neat Tool" is a java based build tool used to compile , assemble  or package source code in a neat presentable way or in simply it is used to build jars and wars.
How it works...
It reads instruction steps, one by one from a XML file and execute them sequentially. These instruction steps are define in terms of some set of keywords (user friendly and easy to understand)
and have specific meaning attached to it e.g.  "property" keyword means name of the variable or "depends" keyword means that something depends on something.
Now let’s get 
started and create a small sample.
First download Ant installable files(zip or tar) from apache ant site.Apache Ant
Since we use ant to build java projects so it is assumed that java is already installed in the system, if java is not there then first download JDK from oracle website and then continue with the ANT setup.
After downloading, unzip the ant folder to any directory (let say in Windows it can be C:\apache-ant-1.8.4 and for Linux it can be /opt/apache-ant-1.8.4) now it’s recommended to set
 "ANT_HOME" environment variable in OS variables and append ANT_HOME/bin; in the existing system path.
In windows go to Environment variable add new variable "ANT_HOME" and set "C:\apache-ant-1.8.4" in value. Now select path from the list of variable and edit its value (at the end add ANT_HOME/bin).
So this will make your ant setup complete. You can go to command prompt ant type ant -version
You will see the version of ant. In case ant is not successfully installed, you will see command not found error.
So now you are ready to dive in. We will create one sample project first and see how ant works.
Let’s create one directory in C drive called sample, this is our working dir.
Need to mention that when we create any java project or write any code, then it is recommended (and looks neat also) to have stuff organized in following directory structure.

  1. src - dir for keeping source code java classes.
  2. build - dir for keeping compiled classes.
  3. lib - dir for keeping dependency

So let us now create above 3 directories in our “sample” dir.
In our src directory put one small java file -  say that prints "hello world"
Now next step is to create build file. Build file is nothing but the above mentioned XML file which ANT will use to read instructions and properties.
First line of the xml file will be
<?xml version="1.0"?>
Which is nothing but just a xml declaration telling which version of xml is being used.
Next is the root element called “project” having 3 basic and mandatory attributes - name , default and basedir.
<project name="test" default="compile" basedir=".">
So first attribute "name" is the name of the project can be anything whatever you feel like.
Next is the default target. Target is nothing but the one modular task that ant performs in one go. Generally one build process can be divided into different smaller modular task
like checking out of latest source code from repository can be termed as one task , deleting and creating directory can be termed as init task or target.
So we can have multiple task or target in a project and these can be latter on weaved together to form one combined task. One target can be composed of single or multiple atomic task.
Further we have 5 attributes attached to target which tell different things about the target.
They are as follows

  1. name- this is a mandatory attribute telling name of the project.
  2. depends -if this target depends on some other target or targets then we need to give it here can be comma separated.
  3. description - again optional
  4. if- this is required if execution of this target depends on some condition.
  5. unless- Optional parameter that adds this target to the dependency list of the specified Extension Point.Extension point is similar to target ,but nothing to execute.

Third attribute is the basedir and every path in build process is relative to this directory.
Now after root tag we can have target tags or variable tags in build xml.
We can define variables in our xml so that they can be used in different targets..say we define our build dir name , and we can use it throughout our xml..they are defined as
<property name ="build" value="build">
and this build variable can be accessed as ${build}.
One more way to define properties is is to define them in a build.property file and add this build.property file in the build.xml (<property file="build.properties"/>)
now lets create one target or modular task.
lets create target called "init"
<target name = "init">
<mkdir dir="${build}"/>
</target>

Now  this init target or modular task is composed of one atomic task which is "mkdir" .now these atomic task have list of predefined attributes or we can say that they are like ant defined functions, we invoke them just by writing names and passing parameters (parameters are passed by giving attributes).

So now our build.xml looks like
<?xml version="1.0"?>
<project name="sample" default="init" basedir=".">
<property name="build" value="build"/>
<target name="init">
<mkdir dir ="${build}" />
</target>
</project>
now if we issue following command
ant , it will create a build dir by executing "init" target.
now for compiling your source code you can have
<javac srcdir="src" destdir="${build}"/>
and for running the Test.java
<java classname="Test"  classpath="${build}"/>
So full list of task can be accessed at Ant Task .

No comments:

Post a Comment