Share on Google+Share on Google+

How to generate build.xml file

This example shows how to generate the build.xml file.

How to generate build.xml file


This example shows how to generate the build.xml file. You may say that build.xml file is the backbone of ANT (Another Neat Tool) technology. Each build.xml file contains only one project name and at least one target. The project tag has only three attributes:







project name

not necessary


target name which called by default

not necessary


the base directory having all path, it contain absolute path

not necessary


<project name="My Project" default="compile" basedir=".">

The project tag is used to create our project and its attributes are used to handle further processing of the project. The name attribute is used to specify the project name and the default attribute is used to specify which target will be called as default when the program will run and the basedir attribute is used to calculate the absolute path of the parent directory. 

An another tag is Target tag which has following five attributes:







target name



depends on another target

not necessary


the name of the property that must be set in order for this target to execute.

not necessary


the name of the property that must not be set in order for this target to execute.

not necessary


short description about target

not necessary


<target name="buildWar" depends="init" description="build a war file"/>

<target name="init" if="build"/>

<target name="init" unless="build"/>

In the target tag, name attribute is used for target name and the depends attribute is used to give sequence of target, i.e., which target will execute first, one target may depend on one or more other targets. The if attribute is used in case of condition, whether property exists or not; then this target will be executed and unless attribute is used as like else condition whether property does not exist, the target will not be executed and the description attribute is used for only giving details about target.

Next tag is property tag which has following four attribute:







name of the property, which is case-sensitive

not necessary


name of task attribute, this is done by placing the property name between "${"name }" in the attribute value



it contain property name

not necessary


name of the property file

not necessary


<property name="build" value="${build}"/> 

<property name="src" location="src"/> 

<property file=""/>

In the property tag, name attribute is used for property name; it is case sensitive. The value tag is used to give the task which will be the name of property in this format "${name}", and the location tag is used to specify the location of task where it performs and the file tag is used to import all properties of ant file. The complete build.xml file structure is as follows:

<project name="My Project" default="jar" basedir=".">

<property name="dir.src" value="src"/>
<property name="" value="build"/>
<property name="dir.dest" value="dest"/>

<target name="clean" description="Removing the all generated files.">
<delete dir="${}"/>
<delete dir="${dir.dest}"/>

<target name="prepare" depends="clean">
<mkdir dir="${}"/>
<mkdir dir="${dir.dest}"/>
<mkdir dir="${dir.src}"/>

<target name="compile" depends="prepare" description="Compilation of all source code.">
<javac srcdir="${dir.src}" destdir="${}"/>

<target name="jar" depends="compile" description="Generates Roseindia.jar file in to the 'dest' directory.">
<jar jarfile="${dir.dest}/roseindia.jar" basedir="${}"/>


The above build.xml file is used to create directory, compile source code, create jar file and clean the directory if already exists. To check the program, simply copy and paste the above code and give appropriate path; then run with ant command on the command prompt. The output is as follows:

The output shows that a jar file named roseindia.jar is created but in this jar file only manifest file is created. When you make a java file in the src folder and run with ant command, the jar file is created completely.

class Hello
  public static void main(String args[]){
  System.out.println("sandeep kumar suman");

To check your program, compile it with ant command on the console; then the following output will be displayed:

Download Source Code



Posted on: September 7, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: How to generate build.xml file   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 26, 2011
Source Code URL Not working

Could you please provide valid Source Code URL?
September 2, 2011
nice explained.

good one !
August 1, 2012

It's well guiding .. Worked perfectly.. Thank you.
November 6, 2012
How to make a build.xml when add a new thirdparty

Hi, Now, i have a thirdparty (Ex: thirdparty.jar). I want to include in my project. My project need thirdparty.jar to run. How can i make the build.xml file? Thanks and best regards, Sren