Java ANT Projects

Java ANT Project 1

Java ANT Examples


Ant Interview Questions And Answers
More interview questions and answers

What is Ant?

Ant is an open source code .It is Java-based build tool sponsored by Apache Software Foundation.It is a program for putting all the pieces of a program together.A simple definition might state that \"Ant is a Java-based build tool from Apache Software Foundation\".Ant is kind of like Make.But there is some difference b/w two.Which we discuss later.

What is a build tool?

A built tool is software which is used to build project,directory structure,copy necessary files to that directory ,compile files ,create jars,set path and class-path ,Build the documentation ,Validate the source code,deploy,debug,and run,clear the workspace.

How many types of build tool you know?

I know two type of build tool which are used to build a java based project.
These are ANT and Make.

What is different between Ant and Make?

The most important difference between Ant and Make is that Ant uses XML to describe the build process and its dependencies, whereas Make uses its Makefile format. By default the ant XML file is named build.xml.

What is History of Ant?

Ant was created by James Duncan Davidson(author of Tomcat). Initially, It was part of the Tomcat .When it was donated to the Apache Software Foundation. Ant was used to build Tomcat, nothing else.But In January 2000, Ant was moved to a separate CVS module and was promoted to a project of its own, independent of Tomcat, and became Apache Ant.

The first version of Ant was the one that shipped with Tomcat\'s 3.1 release on 19 April 2000. This version was referred  as Ant 0.3.1 later.

Today the latest version of ant is released at 27 June 2008 is Ant1.7.1.

Which version of Java is required to run Ant?

The following table lists the minimum Java versions required to compile and run Ant:

Ant Version        Minimum Java Version
1.1  To 1.5.4       1.1
1.6.0 To 1.6.5       1.2
1.7.0 To 1.7.1       1.3
current svn trunk 1.4

Why do you call it Ant?

The ant is acronym of \"Another Neat Tool\" according to James Duncan Davidson. Ants are very small and can carry heavy weight.So as Job of Apache ant. Its name is called ANT.

How you can Prepare a project in ANT?

We can prepare a project by making a build.xml as a build file and using following tag.Inside this tag we have defined standard targets( such as build,clean etc),etc.

<project name=\"NameOfProject\" basedir=\".\" default=\"main\">

From where we can download Ant latest version?

We can download the latest version of ant from the website of Apache ant.The link of website is We need download .zip archive: for window XP/2000/NT etc and  .tar.gz archive: for Linux / Ubuntu /Fedora etc

How we can set the PATH environment variable of ant?

Assume Ant is installed in d:ant. The following sets up the environment:

Set environment for Windows and OS/2

set ANT_HOME=d:ant
set JAVA_HOME=d:jdk-

Set environment for Linux/Unix (bash)

Assume Ant is installed in /usr/local/ant. The following sets up the environment:
export ANT_HOME=/usr/local/ant
export JAVA_HOME=/usr/local/jdk-
export PATH=${PATH}:${ANT_HOME}/bin

Set environment for Linux/Unix (csh)
setenv ANT_HOME /usr/local/ant
setenv JAVA_HOME /usr/local/jdk/jdk-
set path=( $path $ANT_HOME/bin )

How you can clean or delete workspace using Ant?

To clear or delete workspace we need to make a target with name clear .Into this target we are using delete tag and set the name of dir=\"build\" .Here \"build\" is the name of workspace which we have delete or clear .

The Syntax will be as following:

    <target name=\"clean\">
        <delete dir=\"build\"/>

How we can set compile target using Ant? Or How we can compile source using ant?

To compile java application we need to create a target with the name of \"compile\". In this target we need to make a directory where we have to store all the  .class files after compile .Ant have a <javac> tag to compile the java files .In this tag we need to pass two attributes first is destination where we have to store all .class files and other is source directory where all source files are stored .
 The Syntax will be as following:

    <target name=\"compile\">
                  <mkdir dir=�build/classes�>
                 <javac srcdir=�src� destdir=�build/classes�>

How we can create a jar using Ant?

To make a jar of classes we need set target as jar. In this target we need to make directory in which jar will stored. Then we need jar tag to make the jar .In this tag we have pass two attributes first is name of destination directory and second one is the name of base directory where our all class files are stored .We need a manifest to create a jar file. In manifest tag we have pass two attributes first is name of manifest file name and second is its value.

 The Syntax will be as following:

    <target name=\"jar\">
             <mkdir dir=\"build/jar\" />
                 <jar destfile=\"build/jar/Test.jar\" basedir=\"build/classes\">
                     <manifest >
                         <attribute name=\"Main-Class\" value=\"r4r.Test\">

How we can run jar using ant?

To run a jar we need to create a target with the name of run. In this target we need to pass <java> which is used to instruct ant to run application. In this tag we have pass two attributes first is name of jar file with exact location and second is fork=\"true\" or fork=\"false\". The fork=\"true\" mean run into different JVM. The Syntax will be as following: 

<project> ...... .....
<target name=\"run\">
 <java jar=\"build/jar/Test.jar\" fork=\"true\" /> </target>

 ....... .......


How you can compile, jar and run into ant command prompt?

We can compile, package(create jar) and run the application via ant by using following syntax into command prompt ant compileant jarant run Or we can use following as short cut ant compile jar run

What is dependency? How it is used into ant? What is its use?

Dependencies are do something when complete it. In ant we are using dependencies by using an attribute \"depends\" .In this attribute we have pass values for which the target depends .This mean we first need to execute the target which is passed into this attribute.
e.g. In this example we are passing depends=\" clean, compile\". This instruct ant first clean and compile target will executed then clean-build target will executed. 


. . . .
. . .  .
<target name=\"clean\">
        <delete dir=\"${build.dir}\"/>

    <target name=\"compile\">
        <mkdir dir=\"${classes.dir}\"/>
        <javac srcdir=\"${src.dir}\" destdir=\"${classes.dir}\"/>

    <target name=\"clean-build\" depends=\"clean,compile\"/>


How you can explain ant property?

A project can have a set of properties .A property has name and value .The name is case sensitive and Properties are immutable this mean once set property its will not change. 

Properties may be used in the value of task attributes. We can use it by placing name of properties within ${ } in the attribute values.e.g.

    <property name=\"classes.dir\" value=\"classes\"/>

    <target name=\"clean\" description=\"Delete all generated files\">
        <delete dir=\"${classes.dir}\" />


How we can echo message in Ant?

We need to use <echo> tag to print message on ant console. We can use following syntax to print message on ant command prompts.   


    <property name=\"classes.dir\" value=\"classes\"/>




The output will be :${builddir}=classes

Note: To echo $$ use $$$$.

How many ways we can set properties into build ant file?

There are six ways to set properties

Supplying both the name and value attribute.
    <property name=\"src.dir\" value=\"src\"/>
Supplying both the name and refid attribute.
Setting the file attribute with the filename of the property file to load.
Setting the url attribute with the url from which to load the properties.
Setting the resource attribute with the resource name of the property file to load.
Setting the environment attribute with a prefix to use.

We can use the combinations of all above in our build files .But only one should be used at a time.

What is Built-in Properties? And how many Built-in Properties?Explain main.

Ant provides access to all system properties .This system properties build in property is used as they had been defined using a <property> task. E.g, ${} is used to get the os name.
Ant has some main built-in properties:
basedir             This gives the absolute path of the project\'s basedir
ant.file            The absolute path of the build file.
ant.version         The version of Ant    The name of the project executing;    The JVM version Ant detected
ant.home            This is used to get home directory of Ant.

Example Buildfile

<project name=\"R4RAnt\" default=\"dist\" basedir=\".\">
        Simple build-in file


How we can set path PATH and CLASSPATH into an ant build file?

Ant does not need to set classpath.

What is advantage of Ant 1.7.1 ?

Ant 1.7.1 is mainly a bugfix release.

Ant 1.7.1 has extended support for Java6 features.

Ant 1.7.1