Interview Questions

ANT - Simple Ant Build script

In this example, let us see how to create a simple ANT Build XML.

Perform the following steps,
1. Create a Java Project
2. Create a source directory named “src”.
3. Create a “build.xml” file as given below and place inside the Java Project.
4. Right click the build.xml and run as Ant Build. This will compile the Java class and places the class file inside a directory named “classes” directory. Then it executes the Java program.

AntBuildTest Java Class

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

build.xml

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

<target name="run" >
               
        <!-- Create a temporary directory for storing the class file -->
        <mkdir dir="classes"/>
       
        <!-- Compile the Java class -->
        <javac srcdir="src" destdir="classes"/>
       
        <!-- Run the Java class -->
        <java classname="AntBuildTest" classpath="classes"/>
</target>

</project>

Project hierarchy

Java_Project
src/AntBuildTest.java
build.xml

Output

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