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
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Splunk Observability Cloud vs Splunk AppDynamics vs Splunk Cloud Platform

Here’s a clear and updated comparison between Splunk Observability Cloud, Splunk AppDynamics, and Splunk Cloud Platform, highlighting their purpose, strengths, and ideal use cases: 📦 Product Overview…

Read More

Top 10 Virtualization Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction Virtualization software is one of the foundational technologies driving the IT landscape in 2026. With the growing demand for cloud computing, IT professionals and businesses are…

Read More

Top 10 Patch Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction: The Importance of Patch Management in 2026 In 2026, as cyber threats evolve and technology becomes more complex, patch management tools are critical for maintaining cybersecurity…

Read More

Top 10 Remote Monitoring & Management (RMM) Tools in 2026: Features, Pros, Cons & Comparison

Introduction In today’s fast-paced, technology-driven world, businesses rely on the seamless functioning of their IT infrastructure. This makes Remote Monitoring & Management (RMM) tools essential for managing,…

Read More

Top 10 Server Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction Server management tools are essential for businesses in 2026 to maintain the performance, reliability, and security of their IT infrastructure. These tools help streamline the management…

Read More

Top 10 Endpoint Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, businesses of all sizes are increasingly reliant on a variety of devices—laptops, desktops, mobile devices, and other endpoints—that connect to their networks. With the…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
17 days 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