Interview Questions

ANT - Introduction to Ant build Targets

In this article, let us discuss about target in ANT build.

Target

Target is a container which performs set of tasks.

Let us take an example of the below Java program.

AntBuildTest Java Class

public class AntBuildTest
{
        public static void main(String[] args)
        {
                System.out.println("My First Java Program deployed using ANT build target...");
        }
}

In order to deploy the Java application, the following are the set of activities need to be performed.
1. Create a destination directory to store the classes generated during compilation
2. Compile the Java class
3. Run the Java application

In order to perform the above activities, let us create set of targets which does each operation above.

Target creation

<!-- This target is used to create the classes directory to store the generated classes during compilation -->
<target name="createClassDirectory">
        <mkdir dir="classes"/>
</target>

<!-- Compile the Java class -->
<target name="compileJava" depends="createClassDirectory">
        <javac srcdir="src" destdir="classes"/>
</target>

<!-- Runs the Java application-->
<target name="executeJava" depends="compileJava">
        <java classname="AntBuildTest" classpath="classes"/>
</target>

The above targets does all the activities for deploying the application.

If you look at the above targets, each target depends on the other target. Why is it needed? This is because, in order to perform certain operation, there can be some pre-conditions which need to be executed.

Here, target “executeJava” depends on target “compileJava”. This is because, for executing the Java class, first it needs to be compiled.

Target “compileJava” depends on target “createClassDirectory” since destination directory needs to be created for storing the classes generated during compilation.

If there are any dependencies, then a target can depend on another target. A target dependency is an optional one.

The complete build.xml is given below,

<!-- ANT Build.xml -->
<project name="My-First-Build-XML" default="run"  >

<!-- This target is used to create the classes directory to store the generated classes during compilation -->
<target name="createClassDirectory">
        <mkdir dir="classes"/>
</target>

<!-- Compile the Java class -->
<target name="compileJava" depends="createClassDirectory">
        <javac srcdir="src" destdir="classes"/>
</target>

<!-- Runs the Java application-->
<target name="executeJava" depends="compileJava">
        <java classname="AntBuildTest" classpath="classes"/>
</target>

<!-- This is the trigerring point for the build -->
<target name="run" depends="executeJava">
</target>

</project>

Output

Buildfile: E:\proj_drup\workspace\Ant_Proj_1\Build.xml
createClassDirectory:
compileJava:
[javac] Compiling 1 source file to E:\proj_drup\workspace\Ant_Proj_1\classes
executeJava:
[java] My First Java Program deployed using ANT build target...
run:
BUILD SUCCESSFUL
Total time: 1 second