Written by nitinpai on April 16th, 2007
The Great Ant Tutorial – a great jump start
Java, Java EE, Tutorials, XML
54 responses
Apache Ant is a powerful way to convert your developmental structures to deployment structures. It is declarative and all the command line tasks used for deploying an application are represented by simple XML elements. Without much details, this tutorial would breeze you through the steps on how to build a web application using a single XML build file and nothing else. If you have not yet understood what is the use of Ant, read my article on Development and Deployment Structures – the perfect way to build web applications.
I would use the same analogy of my development structure as mentioned in the above linked article i.e. my development structure consists of the following directories:
- web – place for all my JSP, HTML, JavaScripts and Stylesheets. You can provide subdirectories as required for each of the different set of files
- src – place for my java class files consisting of POJO’s or servlets.
- etc – place for all config files like the most common web.xml
- lib - place for all the necessary jar files to run my application. I even have included servlet-api.jar in this directory since you may have to deploy your web application in a remote server after compiling.
- build – a temporary directory for keeping the compiled files
- dist – a place to put in the finally packaged war file (distribution)
- ant- place for my build.xml file and external properties file
All these directories are present in a parent directory called “WebApp“. The three things you have to keep in mind for making an Ant build file are -
- tasks - which correspond to your command line tasks like javac, war etc. A group of tasks can be executed in a sequence by specifying targets.
- target – It is like a function where you put reusable tasks so that it can be called later without duplicating them.
- property – this element is used to define variables in your build file which is useful when the value like project_name or folder_name keeps on changing.
One of the most awesome features of Ant is that you can keep the properties file externally inside of defining all the variables within the build file. This properties file consists of all the required variables and their values in the form of name-value pairs and is simple text file. In this tutorial I would be using an external properties file.
For the tutorial I have used the following:
Okay we are now good to go with the great Ant tutorial. For this make a file with the name “build.properties“. This would be our external variables file.
root.dir=.. lib.dir=lib src.dir=com conf.dir=etc web.content=web project.name=WebApp build.dir=build
NOTE: you must hit the ENTER key for starting a new line after the last line or Ant will give you an error “BUILD FAILED for some unknown reason”
It is not necessary to put in the name with the dot. You can have a name as “projectName” but there should be no quotes for the value. i.e it should NOT be projectName=”WebApp”
To make a build file we have to remember what is to be done for the deployments. To make things simple just make the modules (targets) necessary for the complete web application deployment which are
- clean – remove all prior deployments of the same application if made
- init – make the necessary structure for deployment as per the vendor’s
- compile – compile your servlets or POJO’s
- copy – put the compiled files and web content in the deployment structre as created during init
- war – make the war file and open the browser
Start making the build.xml file in the ant folder as follows:
<project name="AppBuilder" default="war" basedir=".."> <property file="ant/build.properties"/> </project>
Now we have to set the classpath for the servlet-api.jar to compile our servlets so put the servlet-api.jar in the lib directory of your development structure. Check the property “default” in the <project> element. It states the LAST module (target) inside the build file. In our case it is “war”
All the following XML elements will now go inside the <project></project> element created above. So put the classpath setting element,
<path id="classpath"> <fileset dir="${lib.dir}" includes="servlet-api.jar"/> </path>
Here you need to set all the necessary jars to your classpath for successful compilation of the java source files. ${lib.dir} is used to retrive the value of the “lib.dir” i.e lib
Now we will start with out targets (modules) as mentioned in the list above :
1. clean
<target name="clean"> <echo>Cleaning the ${build.dir}</echo> <delete dir="${build.dir}"/> </target>
Here I am removing my build dir in case it was present earlier with compiled files
The <echo> element is only for displaying what you are doing in the command line.
2. init
<target name="init" depends="clean"> <echo>Creating the build directory</echo> <mkdir dir="${build.dir}/WEB-INFclasses"/> <mkdir dir="${build.dir}/WEB-INFlib"/> </target>
Here I am creating the normal deployment structure required for tomcat namely WebApp/WEB-INF/classes, etc. It doesn’t matter if the folder WEB-INF existed before making the folder classes. Ant automatically creates all the necessary parent folders if they don’t exist.
3. compile
<target name="compile" depends="init"> <echo>Compile the source files</echo> <javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes"> <classpath refid="classpath"/> </javac> </target>
Most important step and the most error giving step. Make sure that all the classpath have been set in the above <path> element. If all are proper then the all files in “src.dir” will be compiled successfully and moved to the build\WebApp\WEB-INF\classes. Just check the property “depends” of the <target> element here. This property is used to chain the modules (targets) in a sequential manner.
4. copy
<target name="copy" depends="compile"> <copy todir="${build.dir}/WEB-INF"> <fileset dir="${conf.dir}"/> </copy> <copy todir="${build.dir}"> <fileset dir="${web.content}"/> </copy> <copy todir="${build.dir}/WEB-INF/lib"> <fileset dir="${lib.dir}"/> </copy> </target>
Here I am just copying my complied classes and web content files inside the corresponding deployment structure.
5. war
<target name="war" depends="copy"> <echo>Building the war file</echo> <war destfile="${dist.dir}/${project.name}.war" webxml="${build.dir}/WEB-INF/web.xml"> <fileset dir="${build.dir}"/> </war> </target>
This is the final module (target) in my build.xml file which makes the WebApp.war required for the deployment. “war” is an Ant task for which you provide the path of the web.xml file and the directory which contains the deployment structure i.e in our case “build” directory. The destfile is the final location and name of the war file which would become dist\WebApp.war after the script has run.
Running the above script
Keep the build.properties and build.xml files in the ant folder. Make sure that the ANT_HOME environment variable is set to your ant’s installation bin directory. Now, all you have to do is run the ant command on the build file as,
C:\> cd WebAppant\ant C:\WebAppant\ant> ant
That’s it. If successful your development will finally become a fully packaged WebApp.war.
Final Words
Thus, we have made a complete packaged war file from our source with a single XML file and the wonderful Ant tool which is ready to be deployed in the tomcat manager. We can further dig deeper and make complex build.xml using the complete set of elements that Ant provides. But this tutorial was just to get you started with using Apache Ant. If you have any difficulty in understanding the individual elements please read their detailed explanation in the Apache Ant documentation.
Resources
If you have any problem in making the build.xml or build.properties please download them from here. (Right click and select “Save as ….”)

54 Responses
Monday, April 23, 2007
It’s really a great jump start.
Monday, April 23, 2007
Im glad Thulasi, you found it useful!
Wednesday, April 25, 2007
[...] The Great Ant Tutorial – a great jump start Quick and dirty tutorial. Much cleaner in UI than others and easier to narrow in on the details. (tags: ANT build java apache tutorial) [...]
Friday, June 15, 2007
Hi,
Really its a very great tutorial.
Well, in the build.xml, we need to change the web.xml path. Since will delete all the files, the web.xml file gets disappeared with it builds war file.
By creating com\WEB-INF\web.xml file, I was able to create war file.
Once again, kudos to your effor & approach.
Friday, June 15, 2007
Hi,
Really its a very great tutorial.
Well, in the build.xml, we need to change the web.xml path. Since clean ->’delete dir=”${build.dir}”‘ will delete all the files, the web.xml file gets disappeared with it builds war file.
By creating com\WEB-INF\web.xml file, I was able to create war file.
Once again, kudos to your effor & approach.
Friday, July 13, 2007
great jumpstart!!
Friday, August 3, 2007
Wrong Wrong Wrong and wrong. This tutorial assumes the reader knows everything about deployments and it’s a whole waste of time.
Have a look at this mumbo-jumbo:
“Check the property “default†in the element. It states the LAST module (target) inside the build file. In our case it is “war†”
WTF????
Saturday, August 4, 2007
Murali, Thanks for the tip. But the same ant file created the war for me!
Saturday, August 4, 2007
Stephane, I am not able to understand your agony. I have mentioned in the tutorial that if you are not aware of the deployment structures concept then it has been linked for your reference.
If you are looking for any specific clarification you are free to ask your doubt.
Wednesday, August 29, 2007
Your tutorial was helpful and much appreciated.
Wednesday, September 12, 2007
Was a very good quick start. Did help me.
Monday, October 8, 2007
I’m not a native speaker (?) of the English language, but when writing “inside of defining…” shouldn’t it be “instead of defining….” ?
Monday, October 8, 2007
The section “2. init” code says “/WEB-INFclasses” and “/WEB-INFlib” – shouldn’t it be “/WEB-INF/classes” and “/WEB-INF/lib” ?
Wednesday, October 24, 2007
Good tutorial keep it up.
Great work dude..
Thursday, October 25, 2007
Thanks Sridhar. I’m glad you found it helpful.
Bubba, I think there is some conflict in the Wordpress plugin which displays the code. I will try to correct it. Thanks for bringing it to my notice.
Friday, October 26, 2007
Hi
Really awesome!!! jump start.
very help for the basic start.
Friday, October 26, 2007
This article really helped a beginner like me. I am glad I did find this
Kudos to ur work put into this
Friday, October 26, 2007
Rakesh and Naveen, I am glad it did help you. If you find any tips to improve this tutorial feel free to put in your inputs.
Saturday, October 27, 2007
Ant is something which cannot be learnt from books. Its something we learn with our application and requirement. The style in which Ant is used varies with each developer.
I suggest u to incorporate ANT task for generation of docs as well to make ur example complete
Monday, October 29, 2007
hi Nitin
One thing i have realized by using ANT is that it helps you improving the design of java class packaging, designing loose coupled application etc.
Nitin it would be better if you can provide some more stuff on ant for the situation like when u have dependent class packages environment.
for example ..
if class B of PKG2 extends class A of PKG1 in such scenarios we have to compile class A of PKG1 so that when we call the build.xml file from PKG2 the same complied class would be available.
Also can u please include some more stuff related to few task categories of ANT.
e.g
some conditional stuff or
etc.
Monday, November 26, 2007
I have a even better example in word document. If u want u can make a request at amey_nevrekar@rediffmail.com
Definitely u will get it.
Tuesday, November 27, 2007
Hi nitinpai. This is a very good tutorial and it really helped me. But “in the build.xml, we need to change the web.xml path. Since will delete all the files, the web.xml file gets disappeared with it builds war file. ” It took me some time to find out this. Anyway very useful tutorial.. Thanks a lotttttttt.
Please keep posting like this.
Wednesday, November 28, 2007
From where did you copy this one dude ?? ant tutorial is available in n number of websites, looks like u have cut and pasted something, there are hell a lot of errors in your tut, first correct them
any howz good job pai
Thursday, November 29, 2007
Shekhar,
I am against copy pasting any articles and please don’t raise false accusation on presumptions. A little look here and there would help if you know about Ant articles which are already available. Of course many people work on Ant and would have written about it. I did not create any new Ant Tasks myself. I just have simplified their explanation.
If there are errors please report them.
Monday, January 21, 2008
Really good tutorial. I almost read 3 to 4 tutorial before reading this and it was simple and worthy for beginners. Good STUFF!
Monday, February 11, 2008
Sweet tutorial, just what I have been looking for! Well Pleased…
Wednesday, March 19, 2008
Good one..
I couldnt find any reference of wsejbdeploy command .. can you provide me any info on it
Wednesday, March 19, 2008
It is great. I am new to Ant. It gives us a kick start to use Ant. This foundation is more than enough for going some thing complex with Ant.
Friday, April 18, 2008
This is really a good tutorial for beginners…cheers
Tuesday, April 29, 2008
hi,
good tutorial, but it’s possible to insert de regular expressions to change de code in build-time?
Thanks.
Wednesday, April 30, 2008
@jordi – I am not sure if regular expressions can be used inside Ant or not. If you could state a particular situation where you are required to use expression I’ll try to come up with a solution.
Wednesday, April 30, 2008
hi Nitinpai,
I need to add a buildVersion in my source code, like a string
(String buildVersion =/*[BuildVersion]*/ “date”/*[/BuildVersion]*/), if I want to change this in buildTime I can find the labels “/*[BuildVersion]*/” but the ‘date’ change each re-build. For this I want use the regular expressions.
Thanks.
Thursday, May 1, 2008
@jordi – Have you tried this? http://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html
It looks as though the example is the exact one as you have mentioned.
Wednesday, June 4, 2008
Good One!!! Really helps
Monday, November 3, 2008
so if we are using an IDE such as eclipse or Netbeans, we are sheilded from ant processes you discuss above? i am really new at this and find that i can never really debug things with the IDE’s, e.g. the build file for netbeans is MASSIVE, it makes me so confused… when something goes wrong i don’t know where to begin..
Thursday, December 25, 2008
Hi all,
Thank author very much!
It is a really good tutorial. I followed this and it worked perfectly. If we use IDE like JBuilder or Eclipse, it’s hard to find what’s really inside the deployment. With Ant, everything seems clearer.
Tuesday, February 10, 2009
It is Fantastic!!!
Sunday, March 22, 2009
Thank you so much..wonderful tutorial!
Wednesday, April 1, 2009
Thanks for the article.
Wednesday, April 15, 2009
This could be a good tutorial without some self destructing mistakes insides. At least when I was trying to correct these mistakes I learn t something which was good. I recommended
http://ant.apache.org/manual/CoreTasks/war.html
Wednesday, April 15, 2009
The use was property file was a good idea on the hand deployment structure of the build.xml could have arranged a bit easier.
Thursday, April 16, 2009
@kazi – property files are the best whenever you have to provide some frequently changing details like say username and password for application login, especially in the development stage.
Friday, April 17, 2009
nitinpai- Yes
Friday, June 26, 2009
Nithin,
Thanks a lot! This is a very good jump start article on ‘ant’.
Saturday, July 4, 2009
Thank you for this article.
Friday, July 24, 2009
Hey, this is really great tutorial. I was looking for something like this to start with ant and it helped me a lot.
Friday, July 24, 2009
hey its really great!! i have never found such a great starting material…god bless!!!
Monday, July 27, 2009
Hi Nitin,
Really a good work. !
Thanks a lot …
Wednesday, July 29, 2009
Thanks!
You really put some efforts. I appreciate it thoroughly.
–
Amit Verma
Thursday, August 13, 2009
This is a perfect tutorial to start with.
awesome work!!
this tutorial rocks
Friday, October 23, 2009
Excellent!!!
Author has presented it in a very simple and easy way. Even the minute details also mentioned, which helps a lot if one is stuck with small silly problems.
He as mentioned that also like giving a new line space in property files.
Thanks a lot.
Sunday, December 20, 2009
[...] a build script for this app. “Nothing can be easier” you think and you quickly put together a simple Ant script or a Maven POM file. Your build compiles Java code, runs JUnit tests and creates a WAR file for [...]
Wednesday, December 30, 2009
i could create a war file ( the system cannot specified the path specified pls try to help me
Sunday, January 10, 2010
Thanks a lot. I am just new to ant and it helped me a lot. I am able to create the war file and it is a nice jump start!!
Keep it up in helping beginners like me.
Thanks a lot.