[ Team LiB ] Previous Section Next Section

Recipe 4.5 Creating a WAR File with Ant


You want to use Ant to create a Web ARchive (WAR) file.


Use the Ant war task.


A WAR file is a web application archive that contains servlet classes, JSP files, HTML files, image directories, JAR files, XML configuration files, and other resources that a web application depends on. The WAR is deployed on a web container like Tomcat in order to make the web application available to the container's users. Ant includes a war task that makes it easy to generate a WAR from a directory structure that contains the necessary web application files.

Example 4-6 is a standalone build file that creates a WAR file. It could easily comprise one target in a complex build file that compiles Java files, creates the WAR, and deploys the application (see Recipe 2.6).

This example creates a build sequence of init prepare create-war. The init target creates several properties that refer to directories, such as the build directory containing the servlet class files. The context-path property provides the context path for the web application, and in this case, the name of the WAR file (myapp.war).

You execute this build file from a command prompt whose working directory is the web application's root or top-level directory.

Example 4-6. An Ant file using the war task
<project name="war-task" default="create-war" basedir=".">

    <target name="init" 
        description="Initializes some properties.">

        <echo message="Initializing properties."/>

        <property name="build" value=".\build" />
        <property name="src" value=".\src" />
        <property name="dist" value=".\dist" />
        <property name="lib" value=".\lib" />
        <property name="web" value=".\web" />
        <property name="meta" value=".\meta" />
        <property name="context-path" value="myapp" />


    <target name="prepare" depends="init">

        <echo message=
          "Cleaning up the build and dist directories."/>

        <delete dir="${build}"/>
        <mkdir dir="${build}"/>
        <delete dir="${dist}"/>
        <mkdir dir="${dist}"/>


   <target name="create-war" description=
     "creates a web application archive file" 

       <war destfile="${dist}/${context-path}.war" 

           <classes dir="${build}"/>
           <lib dir="${lib}"/>
           <fileset dir="${web}"/>


If the build file was called war-task.xml, then the Ant file is executed with this command line:

ant -buildfile war-task.xml

The create-war target calls the war task.

The war task's destfile attribute is required; it specifies the location of the resulting WAR file. Example 4-6 creates the WAR in the dist directory. The webxml attribute specifies the location of the web application's deployment descriptor. This web application's web.xml file (in this example) is located in the meta directory.

The example war task has three nested elements: classes , lib, and fileset. The dir attribute of the classes element points to the directory that contains the Java classes that are located in the WEB-INF/classes directory. The war task automatically creates the WEB-INF/classes directory in the WAR file. This task also reproduces all the package-related directories in the build directory when it creates WEB-INF/classes. In other words, if the build directory includes a com/jspservletcookbook directory structure, then the WAR will have the same structure in WEB-INF/classes.

The lib element grabs and stores any JAR files that will be located in the WAR file's WEB-INF/lib directory. Finally, the fileset nested element, in this case, pulls in all the static files and any nested image directories that are contained in /web and places them at the top level of the WAR's directory tree. Here is what the output of this build file looks like (with some editing for readability):

     [echo] Initializing properties.

     [echo] Cleaning up the build and dist directories.
   [delete] Deleting directory 
    [mkdir] Created dir: 
   [delete] Deleting directory 
    [mkdir] Created dir:

      [war] Building war: 

The war task has numerous other optional attributes that are explained in the Ant manual at http://ant.apache.org/manual/CoreTasks/war.html.

See Also

Recipe 4.1 on downloading and setting up Ant; Recipe 4.2 on writing Ant targets; Recipe 4.3 on creating a classpath for an Ant file; Recipe 4.4 on compiling a servlet with Ant; Recipe 4.6 on using Ant to create JAR files; Recipe 4.7 and Recipe 4.8 on starting and stopping Tomcat with Ant; Recipe 2.1 and Recipe 2.6 on deploying web applications using Ant; the Ant manual section on the property task: http://ant.apache.org/manual/CoreTasks/property.html; the Ant manual segment on targets: http://ant.apache.org/manual/using.html#targets; the Apache Ant manual index page: http://ant.apache.org/manual/index.html; the Apache Ant Project: http://ant.apache.org.

    [ Team LiB ] Previous Section Next Section