Introducing ANT
Join the DZone community and get the full member experience.
Join For Freeant is an acronym for another neat tool . it is a build tool. it is used to automate complicated repetitive tasks like setting classpath, compiling the code or packing the compiled files and many more things which you can ever imagine. ant is developed in java so it is platform independent. ant accepts instructions in the form of xml documents thus is extensible and easy to maintain. you don’t need any special editor for writing ant files a normal text editor is enough.
ant installation:
if you are using some ide like eclipse then there is no need to download anything as most of the ide come with ant libraries.
if you are not using any ide and doing all in notepad and you
need to do these things:
steps to set path cmd if you
are not using any ide:
-
download the files from
http://jakarta.apache.org/ant/index.html
and unzip them to some directory.
in our case we are naming the folder as ant. - append /path/to/ant/bin to the path environment variable.
- append the .jar files in /path/to/ant/lib/ to the classpath environment variable. set java_home to point to the location of the jdk installation on the machine that the software is being installed on. append /path/to/jdk/lib/* to the classpath environment variable.
ant file(build.xml) format:
ant file is an xml file. as ant is mostly used for building the
project we name it as
build.xml
.
here you can see a snapshot of the build file. it is written to complie
the code present in src folder.
this is the declaration of the xml file
with its version. all files must start by this.
this project element has three
attributes.
1. name:
here you will write the name of your project
for which you are writing this file.
2. default:
this is the default target to be executed
by ant file.
3. basedir:
this is the base directory(root folder) of
your project. here . means current directory. if this is omitted the
parent directory of the build file will be used.
this explanation is for you. do it
yourself.
this element allows you to write your
own variable with their values. you can define as many properties as you
need. this is a name-value pair where you can access the value of a
property by writing it in $ {<proprtynmae>}.
note: there is no space between $ and { }.
this point has two elements.
1. target:
this element can be
treated as a function which has a set of instructions to do. it has an
attribute name which is used to call the target of the specified name.
2. mkdir:
this element is used to make directries. it
has an attribute dir which takes the name of the directory to be made.
this target compiles the code. here the
depends attribute specifies that this target depends on
init
to
be performed.
javac
uses the system java compliler if
you have not given the classpath explicitly. so to run this you should
have all the java_home and path variables set.
srcdir
is the source directory in which all the source
file(.java) are present and
destdir
is the folder into
which all the complied files(.class) will be created.
writing build files for compiling the project:
here we will write an ant file which will perform a clean build of the project. below you can see snapshot of the build.xml file. in this project we have all the source files in src folder and we have only one file helloworld.java to be complied.
as the code is self explanatory we will leave all the
dicussed topics and will bring new important thing called dependency.
depends:
when a target declares depends attribute then that
target is only executed after the execution of the target on which it
depends.
-
in this case the dependency goes like this
- compile is the default target which the file calls.
- compile target depends on init so init target starts executing.
- now init also depends on clean so the target names clean will be executed before the execution of init.
- so first of all target clean is executed, then init and then compile is executed.
note: you can write more than one names of target in depends attribute like depends=”clean,init” and the execution is from left to right. so first clean will be executed and then init will get executed.be careful while declaring dependency.
running an ant file
if you are using ide like eclipse you can simply right click and select run as –> ant build
if your classpath and code is all correct then you will see the following text in your console
if environment variables like path and java_home is not set in your system than you will get an error as shown below.
so if you get this error message forst set all the variables and then restart the eclipse and run the ant file again.
note: in ant the error messages are well explained so error can be identified easily. if you set the verbose mode by giving the argument -v then you will get detailed log in the console.
from http://himanshugpt.wordpress.com/2010/04/22/introducing-ant/
Opinions expressed by DZone contributors are their own.
Comments