DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Sample Ant Build File - WAR

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 13, 12 · · Tutorial
Like (5)
Save
Tweet
168.99K Views

Join the DZone community and get the full member experience.

Join For Free

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

WAR (file format) Build (game engine) Directory

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • What Is ERP Testing? - A Brief Guide
  • Why I'm Choosing Pulumi Over Terraform
  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo