Ant > Sample Ant Build File - WAR

Sample Ant Build File - WAR

I am using the Spring SimpleFormController example to illustrate the build process. The figure below shows the structure of the web application.

All the classes inside the src directory should be compiled and placed in a separate build/classes directory. The created war file will be placed inside the dist directory.

So first we create the build/classes and the dist directory. The init target does this job.

1.<target name="init">
2.    <mkdir dir="build/classes"/>
3.    <mkdir dir="dist" />

The next step is to compile all the classes in the src directory and place them in the build/classes directory. To do this first you need to add all the lib files inside the "WebContent/WEB-INF/lib" directory to the classpath.

1.<path id="compile.classpath">
2.    <fileset dir="WebContent/WEB-INF/lib">
3.        <include name="*.jar"/>
4.    </fileset>

The target compile uses the javac task to compile the java classes and it depends on the target init, because only when you have the directory ready you can place the classes inside them.

1.<target name="compile" depends="init" >
2.    <javac destdir="build/classes" debug="true" srcdir="src">
3.        <classpath refid="compile.classpath"/>
4.    </javac>

The path we created earlier will be refered here using the <classpath> element.

Now we can create the war file using the war task. To create the war file you need to specify the web directory, lib directory and the classes directory. The destfile attribute specifies the war file location and the webxml attribute specifies the web.xml file location.

1.<target name="war" depends="compile">
2.    <war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
3.        <fileset dir="WebContent"/>
4.        <lib dir="WebContent/WEB-INF/lib"/>
5.        <classes dir="build/classes"/>
6.    </war>

You can use the clean target to clean the project. The clean target deletes the build and the dist directory.

1.<target name="clean">
2.    <delete dir="dist" />
3.    <delete dir="build" />

The complete build.xml file is shown below.

01.<?xml version="1.0" ?>
02.<project name="AntExample1" default="war">
04.    <path id="compile.classpath">
05.        <fileset dir="WebContent/WEB-INF/lib">
06.            <include name="*.jar"/>
07.        </fileset>
08.    </path>
10.    <target name="init">
11.        <mkdir dir="build/classes"/>
12.        <mkdir dir="dist" />
13.    </target>
15.    <target name="compile" depends="init" >
16.        <javac destdir="build/classes" debug="true" srcdir="src">
17.            <classpath refid="compile.classpath"/>
18.        </javac>
19.    </target>
21.    <target name="war" depends="compile">
22.        <war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
23.            <fileset dir="WebContent"/>
24.            <lib dir="WebContent/WEB-INF/lib"/>
25.            <classes dir="build/classes"/>
26.        </war>
27.    </target>
29.    <target name="clean">
30.        <delete dir="dist" />
31.        <delete dir="build" />
32.    </target>

You can download the build file here.

Build file :Download
Project :Download
Contact Us  |  Copyright 2012