How To Build war Files Using Ant

In this tutorial you will see how to create war files using ant.We will learn how to compile source code and generate a war file for our web project.I used ServletDBExample to discuss the build process.

The figure below shows the structure of the web application.

Structure of Project

Ant Installation

In this tutorial you will see how to create war files using ant.We will learn how to compile source code and generate a war file for our web project.I used ServletDBExample to discuss the build process.

The figure below shows the structure of the web application.

Structure of Project

Ant Installation

Creating the build file

Rather than putting all the tasks together in one long list of actions, we’ve broken the separate stages—directory creation,compilation, packaging,and clean-up into four separate targets inside the build file.build.xml is given below.

build.xml
<?xml version="1.0" ?>
<project name="WarExample" default="war">

	<path id="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="classpath"/>
		</javac>
	</target>
	
	<target name="war" depends="compile">
		<war destfile="dist/ServletDB.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>

Let us split the build file line by line so it will become more easy to learn.

This build file adds an init target to do initialization work, which means creating directories.So first we create the build/classes and the dist directory.

Using the <mkdir> task we create build/classes and dist directory.

	<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="classpath">
		<fileset dir="WebContent/WEB-INF/lib">
			<include name="*.jar"/>
		</fileset>
	</path>	

The target compile uses the javac task to compile the java classes.

	
	<target name="compile" depends="init" >
		<javac destdir="build/classes" debug="true" srcdir="src">
			<classpath refid="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/ServletDB.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>

Running the build

Open the command prompt and navigate to the folder where the build.xml resides, and type ant.If the build file has been typed correctly, then you should see the following output on cmd:

Ant Installation

Now open the directory you can see the generated build and dist directory. You can find generated war file inside dist directory.

Download the example







comments powered by Disqus