MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Ant build.xml for WAR Packaging and Tomcat Deployment (Annotated Reference)

Originally published on 2010-07-22; fully revised and updated.

If you maintain or modernize Java web applications that still use Apache Ant, this walkthrough shows how a WAR-oriented build.xml works end to end: from cleanup and compilation to packaging and deployment into a Tomcat webapps directory.

This revision is based on the original sample post: A sample Ant build script that builds a WAR file.

Ant build script WAR file example

WAR build workflow in this Ant script

The script defines two deployment paths: a full WAR build/install path and a faster JSP/static-only refresh path.

flowchart TD
    A[Run Ant target] --> B{Target selected}
    B -->|install| C[clean and prepare]
    C --> D[compile sources]
    D --> E[copy descriptors and resources]
    E --> F[build WAR archive]
    F --> G[copy WAR to app server webapps]
    B -->|jsps| H[copy JSP and static assets only]

Expected properties and project layout

The build relies on conventional directories for source, web pages, resources, and libraries. During packaging, it stages content into a temporary WAR-like folder structure under temp/WEB-INF.

  • src: Java source files
  • src-tests: test or auxiliary Java sources (compiled by this sample)
  • pages: JSP and static web content
  • resources: web.xml, manifest, and app XML files
  • lib: JAR dependencies for compile and WAR inclusion
  • target: final WAR output directory
project-root/
├── build.xml
├── lib/
├── pages/
├── resources/
│   ├── MANIFEST.MF
│   ├── web.xml
│   ├── managed-beans.xml
│   └── navigation-rules.xml
├── src/
├── src-tests/
└── target/

Reference build.xml (based on the original sample)

The following is a cleaned, readable version of the sample logic from the original post.

<project name='MyWebApplication' basedir='..' default='install'>

  <!-- project-specific variables -->
  <property name='jsp.dir.name' value='myapp' />
  <property name='package.name' value='${jsp.dir.name}.war' />
  <property name='webapp.dir' value='/Users/al/tomcat-6.0.16/webapps' />

  <property environment='env' />
  <property name='build.dir' value='build' />
  <property file='${build.dir}/build.${env.HOSTNAME}' />

  <property name='lib.dir' value='lib' />
  <property name='pages.dir' value='pages' />
  <property name='src.dir' value='src' />
  <property name='src.tests.dir' value='src-tests' />
  <property name='resources.dir' value='resources' />
  <property name='dest.dir' value='target' />

  <!-- temporary staging structure -->
  <property name='temp.dir' value='temp' />
  <property name='temp.dir.web-inf' value='${temp.dir}/WEB-INF' />
  <property name='temp.dir.lib' value='${temp.dir.web-inf}/lib' />
  <property name='temp.dir.classes' value='${temp.dir.web-inf}/classes' />
  <property name='temp.dir.meta-inf' value='${temp.dir}/META-INF' />

  <property name='package.file' value='${dest.dir}/${package.name}' />

  <path id='build.class.path'>
    <fileset dir='${lib.dir}'>
      <include name='**/*.jar' />
    </fileset>
  </path>

  <target name='clean'>
    <delete>
      <fileset dir='${dest.dir}' includes='**/*' />
    </delete>
    <delete dir='${temp.dir}' />
    <delete dir='${temp.dir.classes}' />
    <delete dir='${temp.dir.meta-inf}' />
    <delete dir='${temp.dir.web-inf}' />
  </target>

  <target name='prepare' depends='clean'>
    <mkdir dir='${dest.dir}' />
    <mkdir dir='${temp.dir}' />
    <mkdir dir='${temp.dir.lib}' />
    <mkdir dir='${temp.dir.meta-inf}' />
    <mkdir dir='${temp.dir.web-inf}' />
    <mkdir dir='${temp.dir.classes}' />
  </target>

  <target name='compile' depends='prepare'>
    <echo>=== COMPILE ===</echo>
    <javac debug='on' srcdir='${src.dir}' destdir='${temp.dir.classes}' includes='**/*'>
      <classpath refid='build.class.path' />
    </javac>

    <javac debug='on' srcdir='${src.tests.dir}' destdir='${temp.dir.classes}' includes='com/**'>
      <classpath refid='build.class.path' />
    </javac>
  </target>

  <target name='package' depends='compile'>
    <echo>=== PACKAGE ===</echo>

    <copy file='${resources.dir}/MANIFEST.MF' tofile='${temp.dir.meta-inf}/MANIFEST.MF' overwrite='true' />
    <copy file='${resources.dir}/web.xml' tofile='${temp.dir.web-inf}/web.xml' overwrite='true' />
    <copy file='${resources.dir}/managed-beans.xml' tofile='${temp.dir.web-inf}/managed-beans.xml' overwrite='true' />
    <copy file='${resources.dir}/navigation-rules.xml' tofile='${temp.dir.web-inf}/navigation-rules.xml' overwrite='true' />

    <copy todir='${temp.dir.classes}'>
      <fileset dir='${src.dir}'>
        <include name='**/*.xml' />
        <include name='**/*.xsl' />
      </fileset>
    </copy>

    <war destfile='${package.file}' webxml='${temp.dir.web-inf}/web.xml' basedir='${temp.dir}'>
      <fileset dir='${pages.dir}' />
      <lib dir='${lib.dir}' />
      <classes dir='${temp.dir.classes}' />
    </war>
  </target>

  <target name='jsps'>
    <echo>=== DEPLOY JSPS ===</echo>
    <copy todir='${webapp.dir}/${jsp.dir.name}'>
      <fileset dir='${pages.dir}'>
        <include name='**/*.jsp' />
        <include name='**/*.html' />
        <include name='**/*.css' />
        <include name='**/*.gif' />
        <include name='**/*.jpg' />
        <include name='**/*.png' />
        <include name='**/*.js' />
      </fileset>
    </copy>
  </target>

  <target name='install' depends='package'>
    <echo>=== INSTALL ===</echo>
    <copy file='${package.file}' tofile='${webapp.dir}/${package.name}' overwrite='true' />
  </target>

</project>Code language: HTML, XML (xml)

Run the build targets

Use Ant target names directly. Override environment-specific paths with -D properties to avoid editing build.xml per machine.

# Full build and WAR creation
ant package

# Build and copy WAR into Tomcat webapps
ant install -Dwebapp.dir=/opt/tomcat/webapps

# Fast JSP/static-only refresh
ant jsps -Dwebapp.dir=/opt/tomcat/webapps -Djsp.dir.name=myapp

# Clean output directories
ant cleanCode language: PHP (php)

Target comparison

TargetDepends onPrimary outputBest for
cleanNoneRemoves temp and target artifactsResetting state before a new build
preparecleanCreates staging/output directoriesPre-compilation setup
compileprepareCompiled classes in temp/WEB-INF/classesVerifying code compiles
packagecompileWAR file in target/Artifact creation for CI/CD
installpackageWAR copied to server webappsLocal or direct server deployment
jspsNoneJSP/static files copied to deployed app dirFast UI/content-only updates

Common pitfalls and best practices

  • Avoid hard-coded server paths. Externalize webapp.dir using -D properties or environment-specific property files.
  • Keep test outputs separate from production classes. In this sample, src-tests compiles into the same classes directory used in the WAR.
  • Be deliberate with bundled libraries. lib/**/*.jar is broad; verify you are not packaging container-provided JARs.
  • Prefer CI artifact publishing over direct server copy. Use package to generate the WAR, then deploy through your pipeline.
  • Fail fast on missing descriptors. Ensure required files like web.xml exist before packaging.

For legacy Ant projects, this pattern is still workable: keep targets small, parameterize environment values, and treat the generated WAR as the primary deployable artifact.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals

Related Posts

How to setup APM of Java Apps in AppDynamics?

To monitor a Java application in AppDynamics, you attach the AppDynamics Java Agent to the JVM with the -javaagent flag and point the agent at your Controller….

Read More

Jenkins Build and Release Training Blueprint: Git/SVN with Maven and Ant

This revised guide turns a legacy training announcement into a practical learning blueprint for build-and-release engineering. It maps the Jenkins workflow, compares core tools, and includes realistic command and pipeline examples.

Read More

Gradle for DevOps: Core Concepts, Build Workflow, and Practical Examples

Gradle is a flexible build automation tool used to compile, test, package, and publish software in repeatable pipelines. This guide explains how Gradle works, how it compares with Maven and Ant, and how to start with practical commands.

Read More

Apache Ant Training Guide: Curriculum, Build Workflow, and Hands-On Labs

Use this structured Apache Ant training guide to evaluate or design an Ant-focused course for Java build and release engineering. It summarizes the curriculum, demonstrates a realistic build.xml workflow, and compares Ant with Maven and Gradle.

Read More

Apache Ant Interview Questions Answered: Concepts, build.xml Examples, and Practical Snippets

This guide converts a long Apache Ant interview question list into clear, practical answers with working build.xml examples. It covers core Ant concepts, common tasks, debugging, and Ant vs Maven vs Make.

Read More

Maven vs Ant: Key Differences, Build Workflow, and When to Use Each

Maven and Ant both automate Java builds, but they use very different models. This guide compares their workflows, configuration style, and CI implications with practical examples.

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
1 month ago

An Ant build script for creating a WAR file is useful for making Java web application builds repeatable. It is important to include the correct web resources, compiled classes, and required libraries. Cleaning the output directory before each build helps prevent old files from slipping into the package. Testing the WAR file on a local server can catch missing dependencies before deployment. Keeping build paths and library versions clear also makes the script easier to maintain.

1
0
Would love your thoughts, please comment.x
()
x