Core Java
Imal Perera  

Writing Ant Task for Build and Deploy war files

Spread the love

Apache ant is a tool which can be used to automate software build processes.  if I explain this in simple words, lets say you need to build your special project. and lets say to do that first you need to create a copy of the project and then copy some libraries into that copied folder and then you need to change the folder structure by copying few  class files to another folder in the project. and then you need to run a another java program and pass some parameters to it like project name and destination so that it will do some changes to the project and then finally you need to build the project in order to get the jar file or……

So now first you think it is Ok, but while you are working on a real project where you need to write some code and test it.. and again you continue working in the project and after a while you need to test the module you completed by building the project and running it. then you will understand that it is great if you can cut the time you spend to build the project. then the best choice is to write a simple ant task which does everything for you by a simple command.


Apache ant is similar to Make only difference is that ant uses XML for describing the build process where as Make uses Makefile format.

Lets start by installing ant in your machine.

  1. Make sure you have installed java
  2. Download latest Bundle from here
  3. Extract it into Disk
  4. Set environment variables
    JAVA_HOME  ==> path to the jdk
    ANT_HOME  ==> path to
    add ANT_HOME/bin to path variable

 

Now you are ready!!! 😀

In this tutorial i’m covering basics of writing an ant task which will build your Java Dyanamic web project and deploy the generated war file to the tomcat server.  so other than knowing about ant this requires you to have some knowledge of java EE web applications

  1. First Create a dynamic web project in eclipse
  2. Then create a simple jsp page
  3. Right click the project and add empty XML file by doing  New-> Other  -> TYPE word XML -> then give name build.xml

Basics you need to know 

Setting properties, just think these are variables 

<property name=”tomcat” value=”C:/apache-tomcat-7.0.50″ />

Referring a property 

for this we use ${propertyname}

example :

<property name=”tomcat.deployment” value=”${tomcat}/webapps” />

We starts writing ant file by creating <project> tags and this is the scope of the script main attribute we define here is the default target which is the first target that execute when running the ant file

Take a look at this ant file I have created and follow the comments to get better understanding

 




	

	

		

			

		

		

Now we need to run this ant task, you can do it by running the following command

ant -f build.xml

That is it Lets code like ants 🙂

Leave A Comment