How do I build a war file with Ant?
Author: Deron Eriksson
Description: This tutorial describes how to use Ant to build a war (web archive) file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 3 >

The build.xml AntSW script shown below builds a warW file for the 'tomcat-demo' project. This tutorial will walk through this file and how to run the file in EclipseSW.

The name of the project is specified as an attribute of the project tag. This value is used to set the 'project-name' property. Notice that references are expressed using ${...} expression language. A property is essentially a variable, and we have several properties defined at the top of the file.

The 'default' attribute specifies that the "war" target is the default target to run if the script is invoked (although you can invoke any target). This project consists of only one target. A target is an action that we'd like to carry out, and this action can be made up of multiple sub-actions, known as tasks.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="tomcat-demo" default="war" basedir=".">

	<property name="project-name" value="${ant.project.name}" />
	<property name="builder" value="TeamCakes" />

	<property name="war-file-name" value="${project-name}.war" />
	<property name="source-directory" value="src" />
	<property name="classes-directory" value="bin" />
	<property name="web-directory" value="web" />
	<property name="web-xml-file" value="web/WEB-INF/web.xml" />
	<tstamp prefix="build-info">
		<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
		<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
	</tstamp>
	<property name="build-directory" value="build" />

	<target name="war" depends="">
		<mkdir dir="${build-directory}" />
		<delete file="${build-directory}/${war-file-name}" />
		<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
			<classes dir="${classes-directory}" />
			<fileset dir="${web-directory}">
				<!-- Need to exclude it since webxml is an attribute of the war tag above -->
				<exclude name="WEB-INF/web.xml" />
			</fileset>
			<manifest>
				<attribute name="Built-By" value="${builder}" />
				<attribute name="Built-On" value="${build-info.current-date}" />
				<attribute name="Built-At" value="${build-info.current-time}" />
			</manifest>
		</war>
	</target>

</project>

Let's examine the "war" target. Notice that there is a 'depends' attribute. Here, you can specify other targets that should be run before the "war" target is run. In this build.xml file, we don't have any other targets. The "war" target consists of 3 tasks: mkdir, delete, and war. The mkdir task makes the directory to hold our war file if it doesn't already exist. The delete task deletes the war file if it already exists. The war task actually makes our war file.

The warfile attribute of the war task specifies the path and file name of the war file to create. The webxml attribute specifies where our web.xmlW to be used is located. Within the war task, the classes tag specifies the location of our project's JavaSW classes to be placed in the war file. The fileset tag specifies the other files that should be placed in the war file. Since the lib directory is in WEB-INF in our project, we don't need to add anything else to copy any jarW files into our war file, since the fileset tag takes care of lib for us given this project's structure. The exclude tag excludes WEB-INF/web.xml from the fileset since it is handled by the war task's webxml attribute. This exclude task isn't really necessary but it does prevent an Ant warning message.

The manifest tag allows data to be written to the META-INF/MANIFEST.MF file within the war file. The script outputs who built the war file, the date that the war file was built, and the time that the war file was built.

(Continued on page 2)

Page:    1 2 3 >