Ant Scripts

In this post I want to share few things about Ant Scripts.
Why do we need an Ant script
I feel the ultimate use of this scripting language is compiling/building a large collection of source files as a batch in a controlled manner.
What is an Ant Script An Ant script is an XML configuration file that calls out a target tree where various tasks get executed with the basic aim of building source files. Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an attribute and can later be referred to by the value supplied to this. The value has to be unique.
Target A Target holds a collection of tasks and may depend on other tasks. The dependency among the targets are specified using the depends attribute. Multiple targets can be specified using comma as separator. The flow of Ant project depends on the starting/First target that is called.
Commonly used targets are compile, test, clean etc.,
Example: Below one is a fine example that illustrates the flow and dependency among the targets.
target name="A"
target name="B" depends="A"
target name="C" depends="B"
target name="D" depends="C, B, A"
Suppose we call the target D first. The following holds good.
  • A is the first target that is executed
  • Order of execution is A->B->C and finally D.
  • Each target is executed only once.

Task A Task does the work of the Ant script, such as running javac, java etc.
Each Ant build project mainly contains two segments .First one that property setting section that specifies the classpath, Base directory etc and the Second one actual targets.

Installing Ant
Check whether Ant installed on your machine by running the command “Ant” in the command prompt. If it outputs something like this you need install Ant.

1)Ant download is not a installable application.It includes a set of executables,library files etc that can be copied to any desired location.
2)Ant can be downloaded from the Apache website that regulates it. Below is the pointer to it. Ant Download.
3)Now extract all the files to a directory.I usually extract them to "c:\Ant" directory.
4)Make sure Java is installed on your machine.( Say the path is " C:\jdk1.3.1")
5)Now add these as part of the environment variables.To do it ,right click on the 'My Computer'.Navigate to 'Advanced Tab',Click 'Environment Variables' and try to add new.
6)Try to add the below two environment variables "ANT_HOME" and "JAVA_HOME".For assistance see the below pictures.








Now try to run the “Ant -v“at the command prompt. It should output the version.

Additional Plugin Jars
Ant doesn’t support certain tasks or attributes that are often needed in easy developing of build scripts. For example the task repeatedly executes a series of tasks that does compilation or execution. Ant doesn’t come with this support. So we need to add it to the library of jars in the ANT_HOME manually.
Here we need “ant-contrib-0.2.jar”. Here is the pointer to the jar file
http://cvs.sourceforge.net/viewcvs.py/xmoon/struts-demo/WEB-INF/lib/ .

Note: The above jar doesn’t support trim attribute.

Basics of Ant Usage
By now assume Ant s properly installed and the Paths are set properly. Now lets see how it can be used.
Ant is usually executed at the command prompt. The following points demo the usage of Ant.
1) By just running the Ant at the command prompt, it assumes Build.xml as the default argument and tries to run the build.xml file located in the current directory.
i.e. Ant at command prompt is similar to “Ant Build.xml”

2) To run a user defined build file say build_abc.xml the ‘-buildfile’ option need to be used.
Ex: Ant -buildfile build_abc.xml

3) Now lets see how the targets inside the build file are called once we invoke the buildfile.The first target with which the execution to be started is usually said using the default attribute in the project definition tag.
Ex: - <project name="EnterpriseOne" default="dist" basedir=".">
In the above case default attribute says dist is the first target to be called by default. Ant will determine the flow among the targets based on the depends attribute specified in the dist target.

4) To call a specific target, the name of the target should be specified as an argument.
Ex:
1) “Ant execute” asks the Ant to start with target execute first.(This is for default build.xml)
2) “Ant –buildfile build_abc.xml execute”, means the target named execute is called first from the build_abc.xml.

More Info
Ant Resources
Ant Download


No comments: