Creating WAR files with Ant is extremely simple, and very similar to the creating JAR files task. After all, WAR file, like JAR file is just another ZIP file.

    The WAR task is an extension to the JAR task, but it has some nice additions to manipulate what goes into the WEB-INF/classes folder, and generating the web.xml file. The WAR task is useful to specify a particular layout of the WAR file.

    Since the WAR task is an extension of the JAR task, all attributes of the JAR task apply to the WAR task.

    Sr.No. Attributes & Description


    Path to the web.xml file



    A grouping to specify what goes into the WEB-INF\lib folder.



    A grouping to specify what goes into the WEB-INF\classes folder.



    Specifies the instructions for generating the MANIFEST.MF file.

    Continuing our Hello World Fax Application project, let us add a new target to produce the jar files. But before that let us consider the war task. Consider the following example −

    1. <war destfile = "fax.war" webxml = "${web.dir}/web.xml">
    2. <fileset dir = "${web.dir}/WebContent">
    3. <include name = "**/*.*"/>
    4. </fileset>
    6. <lib dir = "thirdpartyjars">
    7. <exclude name = "portlet.jar"/>
    8. </lib>
    10. <classes dir = "${build.dir}/web"/>
    11. </war>

    As per the previous examples, the web.dir variable refers to the source web folder, i.e, the folder that contains the JSP, css, javascript files etc.

    The build.dir variable refers to the output folder - This is where the classes for the WAR package can be found. Typically, the classes will be bundled into the WEB-INF/classes folder of the WAR file.

    In this example, we are creating a war file called fax.war. The WEB.XML file is obtained from the web source folder. All files from the ‘WebContent’ folder under web are copied into the WAR file.

    The WEB-INF/lib folder is populated with the jar files from the thirdpartyjars folder. However, we are excluding the portlet.jar as this is already present in the application server’s lib folder. Finally, we are copying all classes from the build directory’s web folder and putting into the WEB-INF/classes folder.

    Wrap the war task inside an Ant target (usually package) and run it. This will create the WAR file in the specified location.

    It is entirely possible to nest the classes, lib, metainf and webinf directors so that they live in scattered folders anywhere in the project structure. But best practices suggest that your Web project should have the Web Content structure that is similar to the structure of the WAR file. The Fax Application project has its structure outlined using this basic principle.

    To execute the war task, wrap it inside a target, most commonly, the build or package target, and run them.

    1. <target name = "build-war">
    2. <war destfile = "fax.war" webxml = "${web.dir}/web.xml">
    3. <fileset dir = "${web.dir}/WebContent">
    4. <include name = "**/*.*"/>
    5. </fileset>
    7. <lib dir = "thirdpartyjars">
    8. <exclude name = "portlet.jar"/>
    9. </lib>
    11. <classes dir = "${build.dir}/web"/>
    12. </war>
    13. </target>

    Running Ant on this file will create the fax.war file for us.

    The following outcome is the result of running the Ant file −

    1. C:\>ant build-war
    2. Buildfile: C:\build.xml
    5. Total time: 12.3 seconds

    The fax.war file is now placed in the output folder. The contents of the war file will be −

    1. fax.war:
    2. +---jsp This folder contains the jsp files
    3. +---css This folder contains the stylesheet files
    4. +---js This folder contains the javascript files
    5. +---images This folder contains the image files
    6. +---META-INF This folder contains the Manifest.Mf
    7. +---WEB-INF
    8. +---classes This folder contains the compiled classes
    9. +---lib Third party libraries and the utility jar files
    10. WEB.xml Configuration file that defines the WAR package