Ant WAR task - A sample Ant build script that builds a WAR file

You'll probably have to know a little bit about Java, Ant, and build files for this sample build.xml file to be any use to you, but if you're looking for a sample Ant build script that can be used to create a war file, or one that simple uses a war task, this example might work for you.

I'm not going to provide any explanation of any of this right now, but if I have time in the future I'll come back here and add a few comments about how this build script works.

An Ant WAR task example

In the meantime, here is a sample Ant build.xml file that demonstrates the use of the Ant WAR task, and can be used to create a war file.

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

    <!-- project-specific variables -->
    <property name="" value="myapp" />
    <property name="" value="${}.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" />

    <!-- put everything in a temp folder with the right structure during the build -->
    <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}/${}" />

    <path id="build.class.path">
        <fileset dir="lib">
            <include name="**/*.jar" />

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

    <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}" />

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

        <!-- compile files on the src-tests path -->
        <echo>Compiling ${src.tests.dir} files ...</echo>
        <javac debug="on" srcdir="${src.tests.dir}" destdir="${temp.dir.classes}" includes="com/**">
            <classpath refid="build.class.path" />

    <!-- PACKAGE -->
    <target name="package" depends="compile">
      <echo>=== PACKAGE ===</echo>
      <!-- copy the config files -->
      <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"/>
      <!-- the ant war task. with all resources in place, create the war file -->
      <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}" />

    <!-- JUST DEPLOY THE JSP's (without rebuilding the entire project) -->
    <target name="jsps">
      <echo>=== DEPLOY JSP'S ===</echo>
      <!-- i'm trying to be explicit about what i put out there -->
      <copy todir="${webapp.dir}/${}">
        <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"/>
    <!-- INSTALL -->
	<target name="install" depends="package">
      <echo>=== INSTALL ===</echo>
      <copy file="${package.file}" tofile="${webapp.dir}/${}" overwrite="true" />


Okay, one note ... you'll see that I also created a task just to deploy my JSP files. When working just on my JSP files, I always find it much faster to just copy my JSP files to the webapps folder, as compared to building the entire project, so that's where this task comes from.

I hope this Ant WAR task example is helpful.

I need to document that task better, but I just use this task whenever I want to deploy the JSPs without rebuilding the entire project. When I'm working on things that are strictly user interface changes (HTML, CSS, JavaScript, new images), I just type:

ant jsps

and then click refresh in my browser. This is a much faster process than rebuilding the entire project.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.