Recently, I gave a presentation to my colleagues on Apache Ant. Since most of them are freshers, I had to explain the usage of Ant using a very simple example – Hello Ant (what else will it be!?). I thought of writing about the Apache Ant in my blog also.
What is Apache Ant?
- Ant is a build tool from Apache. It is a simple XML file that tells how your project should be built.
- The XML file (build file) is named as build.xml.
- Ant is open source and as it has been developed using Java, it’s cross-platform.
How do I use Ant?
- I hope that you are already using some kinda IDE (Eclipse, Netbeans, etc) for your development (what !? Notepad !!? still !!!?). These IDEs, for example Eclipse, supports Ant tool out of the box.. No need to install anything separately (anyhow, you need to configure JDK with Eclipse properly).
- Ant view can be opened in Eclipse, be selecting Window->Show View -> Others -> Ant
- Once the Ant view is opened, you can drag-and-drop your build.xml (ant script file) over the Ant view. The view will detect the targets (see the next section) that are present in the Ant script file and it will show them. You can execute any target by double clicking on it.
Tasks,Targets and properties in Ant:
- A task is nothing but a command that you usually executes in command line. For example javac, mkdir, copy, etc.
- You can combine a set of tasks into targets. When you run a target, all the tasks that are present inside that target will be executed. For example, the target clean may contain a task to delete the build directories.
- You can define properties in your ant script. Properties are similar to the constants that we use in our Java code. For example, you can define a property named as build.dir and you can initialize it to “build” and in your ant script you can use the property build.dir where ever you wanna specify the output directory location.
A simple Ant build.xml file:
That said, now lets have a look at a simple Ant build.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project name="helloant" default="build-jar" basedir=".">
<property name="src.dir" value="src" />
<property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="jar.dir" value="${build.dir}/jar" />
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" />
</target>
<target name="build-jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" />
</target>
</project>
- The second line creates a new project named “helloant” and then sets the default target for this project is “build-jar”. i.e. When you run this build.xml file, with out explicitly specifying which target you wanna run, the Ant compiler will run the “build-jar” target.
- The next four lines defines the properties that will be used inside the Ant script. You can define any number of properties that you want and you can access the System properties too.
- Then we’ve defined a target named “clean”, inside which, there is a task to delete the directory ${build.dir}. Note how we are accessing the value of the property, using the Expression Language syntax.
- Similarly, we’ve defined targets “compile” and “build-jar”. Also note the keyword depends in the <target> tag. This means that when you directly run the “compile” target, the Ant compiler will first runs the “clean” target as, “compile” depends on “clean”.
- To run this script, just drag-and-drop it on the Ant view and double click on the target, you want to run.
Hello Ant is over, what next !?
Of course, Ant can do much more than this simple program. You can see the complete list of tasks that Ant can perform in Apache’s Ant website. I hope that this basic introduction of Ant helped you to get a overview of what the Ant is.
Comments on this entry are closed.