Meyyappan has posted 101 posts at DZone. View Full User Profile

Sample Ant Build File - WAR

06.13.2012
| 86709 views |
  • submit to reddit

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.

<target name="init">
    <mkdir dir="build/classes"/>
    <mkdir dir="dist" />
</target>

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

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

 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.

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

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.

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

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

<target name="clean">
    <delete dir="dist" />
    <delete dir="build" />
</target>

 The complete build.xml file is shown below.

<?xml version="1.0" ?> 
<project name="AntExample1" default="war">

	<path id="compile.classpath">
		<fileset dir="WebContent/WEB-INF/lib">
			<include name="*.jar"/>
		</fileset>
	</path>
	
	<target name="init">
		<mkdir dir="build/classes"/>
		<mkdir dir="dist" />
	</target>
	
	<target name="compile" depends="init" >
		<javac destdir="build/classes" debug="true" srcdir="src">
			<classpath refid="compile.classpath"/>
		</javac>
	</target>
	
	<target name="war" depends="compile">
		<war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
			<fileset dir="WebContent"/>
			<lib dir="WebContent/WEB-INF/lib"/>
			<classes dir="build/classes"/>
		</war>
	</target>
	
	<target name="clean">
		<delete dir="dist" />
		<delete dir="build" />
	</target>
	
</project>

You can download the build file here.

Build File: Download

Project:  Download

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Sreenivas Sama replied on Thu, 2014/06/19 - 7:19pm

wonderful again.... 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.