RSS

Introducing ANT

22 Apr

ANT is an acronym for Another Neat Tool

It is a build tool. It is used to automate complicated repetitive tasks like setting classpath, compiling the code or packing the compiled files and many more things which you can ever imagine. ANT is developed in JAVA so it is platform independent. Ant accepts instructions in the form of XML documents thus is extensible and easy to maintain. You don’t need any special editor for writing ANT files a normal text editor is enough.

ANT Installation:

If you are using some IDE like Eclipse then there is no need to download anything as most of the IDE come with ANT libraries.

If you are not using any IDE and doing all in notepad and you need to do these things:
Steps to set path cmd if you are not using any IDE:

  1. Download the files from http://jakarta.apache.org/ant/index.html and unzip them to some directory.
    In our case we are naming the folder as ant.
  2. Append /path/to/ant/bin to the PATH environment variable.
  3. Append the .jar files in /path/to/ant/lib/ to the CLASSPATH environment variable. Set JAVA_HOME to point to the location of the JDK installation on the machine that the software is being installed on. Append /path/to/jdk/lib/* to the CLASSPATH environment variable.

ANT file(build.xml) format:

ANT file is an XML file. As ANT is mostly used for building the project we name it as build.xml.
Here you can see a snapshot of the build file. It is written to complie the code present in src folder.

This is the declaration of the XML file with its version. All files must start by this.

This project element has three attributes.
1. name: Here you will write the name of your project for which you are writing this file.
2. default: This is the default target to be executed by ant file.
3. basedir: This is the base directory(root folder) of your project. Here . means current directory. If this is omitted the parent directory of the build file will be used.

This explanation is for you. DO IT YOURSELF.

This element allows you to write your own variable with their values. You can define as many properties as you need. This is a name-value pair where you can access the value of a property by writing it in $ {&ltproprtyNmae>}.
NOTE: THERE IS NO SPACE BETWEEN $ AND { }.

This point has two elements.
1. Target: This element can be treated as a function which has a set of instructions to do. It has an attribute name which is used to call the target of the specified name.
2. mkdir: This element is used to make directries. It has an attribute dir which takes the name of the directory to be made.

This target compiles the code. Here the depends attribute specifies that this target depends on init to be performed.javac uses the system java compliler if you have not given the classpath explicitly. So to run this you should have all the JAVA_HOME and PATH variables set.
srcdir is the source directory in which all the source file(.java) are present and destdir is the folder into which all the complied files(.class) will be created.

Writing Build Files for compiling the project:

Here we will write an ant file which will perform a clean build of the project. Below you can see snapshot of the build.xml file. In this project we have all the source files in src folder and we have only one file HelloWorld.java to be complied.

<?xml version="1.0" encoding="UTF-8"?>

<!--  Project name Declaration -->
<project name="HelloWorld" basedir="." default="compile">

	<!-- Setting the property here -->
	<property name="bin" value="bin" />

	<!-- This target makes a fresh directory if not present -->
	<target name="init" depends="clean">
		<mkdir dir="${bin}" />
	</target>

	<!-- Delete the old directory with all its files -->
	<target name="clean">
		<delete dir="${bin}" />
	</target>

	<!-- Compiles the source files -->
	<target name="compile" depends="init">
		<javac srcdir="src" destdir="${bin}" />
	</target>
</project>

As the code is self Explanatory we will leave all the dicussed topics and will bring new important thing called dependency.
depends:
When a target declares depends attribute then that target is only executed after the execution of the target on which it depends.

    In this case the dependency goes like this

  1. Compile is the default target which the file calls.
  2. Compile target depends on init so init target starts executing.
  3. Now init also depends on clean so the target names clean will be executed before the execution of init.
  4. So first of all target clean is executed, then init and then compile is executed.

Note: You can write more than one names of target in depends attribute like depends=”clean,init” and the execution is from left to right. So first clean will be executed and then init will get executed.Be careful while declaring dependency.

Running an ANT file

If you are using IDE like eclipse you can simply right click and select Run As –> ANT Build

If your classpath and code is all correct then you will see the following text in your console

If Environment variables like PATH and JAVA_HOME is not set in your system than you will get an error as shown below.

So if you get this error message forst set all the variables and then restart the eclipse and run the ant file again.


Note: In ant the error messages are well explained so error can be identified easily. If you set the verbose mode by giving the argument -v then you will get detailed log in the console.

About these ads
 
Leave a comment

Posted by on April 22, 2010 in ANT, Java

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: