ANT script To Compile and create war file – Tutorial

Posted: April 27, 2012 in ANT, Configuration, Tutorials
Tags: , , , , , , ,

Today we will learn how to compile source code and genrate a war file for our web project.

[Desired WAR Structure]

First of all I will explain the structure of WAR file which is deployable on jboss. (here i will take an exmaple of spring MVC web project).

for that we need to understand the structure of war file. in following listing I have mentioned important part of the war file.

  |   index.jsp
  + - META-INF
  + - WEB_INF
  	|   disp-servlet.xml
  	|   web.xml
  	+ - classes
  	|	(all class files)
  	+ - jsp
  	|	(jsp i have used for mvc)
  	+ - lib
  		(all required jar files - spring releated etc)

Structure of Project

Here is how my project is structured in eclipse, Please note that i will not mention eclipse related files (configuration etc.)

  + - src
  |      +com.mazhar.spring
  + - WebContents
		| - index.jsp
		+ - META-INF
		+ - WEB_INF
			| -  disp-servlet.xml
			| -  web.xml
			+ - jsp
				- welcome_maszhar.jsp
				- goodbye_maszhar.jsp


Now we understand what is the structure of our project and what will be strtucture of our resulatant war file.
We know that our java files need to be compiled into class files and required(dependent) jar files need to be included into the war file,so that application run smoothly on Jboss Server. Hence following would be our steps for genrating a jar file.

1 - First step is to crate a build.xml file in same project space, which will have all the required instructions to run an ANT Script.

2 - We will also crate another file "" in same folder, this will hold our common settings and paths to different location.
	* It will have a property that point to jboss lib folder.
	* It will have a property that point to jboss deploy folder.
	* etc.
6 - We will create another folder locallib in same project.
	* It will keep all spring fromework relevant jar files or any other jar file which is requred to complie this project and also will be required to run this application on jboss (becuase we will packge these jars into our WAR file as well).

3 - In build.xml file we will define our project and few variables that point to src, webContent, build folder etc.

4 - Next we will define our class path.
	* In this section we will mention different locatiions for external jars that are required to compile our project.
	* like my custom lib folder which holds all the spring framework related jar files.
	* like a servlet-api.jar file(s) that are placed in jboss lib folder.
	* etc
5 - We will define some targets which will be called from command line.
	* example
		ant clean
		ant build

6 - ANT script will perform following steps
	6.1 It will create "build" folder within the same project space, where all of the complied classes will be genrated.
	6.2 Now it will compile java files from src folder and keep their genrated class files in build/classes/ folder.
	6.3 Now ANT script will create a war file by including contents of the webContent folder.
		* ANT script will also include all jar files from locallib into SpringWebExample.war/WEB-INF/lib folder.
		* ANT script will also include all class files from build/classes into SpringWebExample.war/WEB-INF/classes folder.

7 - We can find our SpringWebExample.war in build folder.

Here is how out war file look like

+ Classes
+ com.mazhar.spring
+ Jsp
+ lib







<?xml version="1.0" encoding="UTF-8"?>
<project name="SpringWebExample" basedir="." default="usage">
    <property file=""/>
    <property name="src.dir" value="src" />
    <property name="web.dir" value="WebContent" />
    <property name="build.dir" value="${basedir}/build" />
    <property name="name" value="SpringWebExample" />

    <path id="project-classpath">

    	<!--  here give path for jar repository -->
    	<fileset dir="${server.lib}">
    		<include name="servlet*.jar" />
    	<fileset dir="${basedir}/locallib">
    		<include name="*.jar" />
    	<pathelement path="${build.dir}" />

    <target name="build" depends="compile" description="build project, but first compile it">
         <war destfile="build/${name}.war"
             <lib dir="${basedir}/locallib"/>
             <classes dir="${build.dir}/classes" />
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>

    <target name="compile" description="Compile java files and place them webContents/web-inf/">
    	<mkdir dir="${build.dir}/classes"/>
        <javac destdir="${build.dir}/classes" failonerror="true">
            <src path="${src.dir}"/>
            <classpath refid="project-classpath"/>

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


cmd>ant clean build


cmd>ant clean
cmd>ant build
  1. Sree says:

    Not so impressive

  2. kapil says:

    Default target usage does not exist in the project

  3. Mohammed says:

    Thank you

  4. neoleixz says:

    Thank you, right for me as a starter.

  5. Excellent amazing says:

    Excellent amazing

  6. venkatesh says:

    wow wonderful

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s