Build Automation #4 - Understanding the Ant script
The build.xml in the example that you downloaded in the previous post contains the Ant script. Lets go through it to understand the key elements of the Ant script. Ofcourse it is not necessary that the ant script should be in a file called build.xml. If it is named something else then you need to tell ant which file it is when you run ant. And there could be multiple ant scripts that could be linked as well. You can learn about it from the Ant manual. Open up the build.xml file in an editor.
The very first element in any ant script is the project element. The project element contains the following attributes.
name- Name of the project
default- Default target to execute.
basedir- Which directory in the system should be taken as the base directory. If not provided the current directory from where Ant is executed will be treated as the base directory.
The below code is from our HelloWorld sample.
<project name="Hello World" default="package" basedir=".">
Ant basically works with targets. A target is a specific goal to be attained. A target contains set of tasks that needs to be executed in order to attain the goal. A target could dependent on one or more targets, in which case the dependent targets are executed first before this target gets executed. Lets take the clean target from our example.
<target name="clean" description="remove all ant generated files/folders">
<delete dir=”${target.dir}”/>
</target>
Of importance to us in the target element are three attributes - name, description and depends. The depends attribute holds other targets separated by commas that this target is dependent upon. In the above example, the clean target is not dependent on any other target. Within the target element you write all the tasks necessary to complete that target. A task could be a built in task or custom written. A task is a finite piece of code that can be executed. In the above example the clean target has one task which is delete. The delete task is a built in Ant task and its purpose is to delete files/folders in the system. Essentially the clean target’s goal is to clean up previous build artifacts. In our simple example, the target folder under the HelloWorld is where all the compiled classes and the final HelloWorld.jar gets placed. So basically the clean target aims at removing the target folder.
The delete target in our example has an attribute dir. Note the value we pass to the dir attribute. In Ant anything represented within ${} is treated as a variable and gets replaced with the value when the script executes. In our case target.dir is a property that we have defined in the beginning of the script.
<property name="target.dir" value="${basedir}/target"/>
A property is like declaring a variable. You could also put all your properties in a property file and refer the property file like,
<property file="build.properties"/>
and the build.properties containing the following.
src.dir=${basedir}/src
test.dir=${basedir}/test
lib.dir=${basedir}/lib
target.dir=${basedir}/target
classes.dir=${target.dir}/classes
Note that since it is a property file we define a property in property-name=property format.
With this information in mind, I am sure you would be able to understand the entire build script. Now the target dependency needs some more explanation. So when we call ant, what is the sequence in which the build is executed. Remember in our project element we gave the default target to be package. And if you look at the package target, it is dependent on unit-test target. If you tailgate behind the dependent targets, you would see this leads to the clean target in the following order.
package -> unit-test -> build -> create -> clean
So the execution begins from a target that is independent of any other target. So the execution of the targets would be like below.
clean -> create -> build -> unit-test -> package
It is always not necessary that you have to run the default target, you can tell ant to execute a specific target but just giving the target name when executing. Say for example I just want to compile the classes, then I give the following command.
c:\HelloWorld\>ant build
In this case ant focuses on the build target, tracks the dependent targets and executes them like below.
clean -> create -> build
In our case the target execution was simple and each target was dependent on only one target, but what if a target is dependent on more than one target. What will be the sequence in that case? Well go through the ant manual here to understand about it.












