## War

### Description

An extension of the Jar task with special treatment for files that should end up in the `WEB-INF/lib`, `WEB-INF/classes` or `WEB-INF` directories of the Web Application Archive.

(The War task is a shortcut for specifying the particular layout of a WAR file. The same thing can be accomplished by using the prefix and fullpath attributes of zipfilesets in a Zip or Jar task.)

The extended zipfileset element from the zip task (with attributes prefix, fullpath, and src) is available in the War task. The task is also resource-enabled and will add nested resources and resource collections to the archive.

Before Servlet API 2.5/Java EE 5, a WEB-INF/web.xml file was mandatory in a WAR file, so this task failed if the `webxml` attribute was missing. As the web.xml file is now optional, the `webxml` attribute may now be made optional. However, as most real web applications do need a web.xml file, it is not optional by default. The task will fail if the file is not included, unless the `needxmlfile` attribute is set to `false`. The task will warn if more than one web.xml file is added to the JAR through the filesets.

Please note that the Zip format allows multiple files of the same fully-qualified name to exist within a single archive. This has been documented as causing various problems for unsuspecting users. If you wish to avoid this behavior you must set the `duplicate` attribute to a value other than its default, `"add"`.

### Parameters

Attribute | Description | Required
destfile | the WAR file to create. | Exactly one of the two.
warfile | Deprecated name of the file to create -use `destfile` instead.
webxml | The servlet configuration descriptor to use (WEB-INF/web.xml). | Yes, unless `needxmlfile` is true, the file is pulled in via a nested fileset, or an existing WAR file is being updated.
needxmlfile | Flag to indicate whether or not the web.xml file is needed. It should be set to false when generating servlet 2.5+ WAR files without a web.xml file. Since Apache Ant 1.7 | No -default "true"
basedir | the directory from which to jar the files. | No
compress | Not only store data but also compress them, defaults to true. Unless you set the keepcompression attribute to false, this will apply to the entire archive, not only the files you've added while updating. | No
keepcompression | For entries coming from existing archives (like nested zipfilesets or while updating the archive), keep the compression as it has been originally instead of using the compress attribute. Defaults false. Since Ant 1.6 | No
encoding | The character encoding to use for filenames inside the archive. Defaults to UTF8. It is not recommended to change this value as the created archive will most likely be unreadable for Java otherwise. See also the discussion in the zip task page | No
filesonly | Store only file entries, defaults to false | No
includes | comma- or space-separated list of patterns of files that must be included. All files are included when omitted. | No
includesfile | the name of a file. Each line of this file is taken to be an include pattern | No
excludes | comma- or space-separated list of patterns of files that must be excluded. No files (except default excludes) are excluded when omitted. | No
excludesfile | the name of a file. Each line of this file is taken to be an exclude pattern | No
defaultexcludes | indicates whether default excludes should be used or not ("yes"/"no"). Default excludes are used when omitted. | No
manifest | the manifest file to use. | No
filesetmanifest | behavior when a Manifest is found in a zipfileset or zipgroupfileset file is found. Valid values are "skip", "merge", and "mergewithoutmain". "merge" will merge all of the manifests together, and merge this into any other specified manifests. "mergewithoutmain" merges everything but the Main section of the manifests. Default value is "skip". | No
whenmanifestonly | behavior when no files match. Valid values are "fail", "skip", and "create". Default is "create". | No
update | indicates whether to update or overwrite the destination file if it already exists. Default is "false". | No
duplicate | behavior when a duplicate file is found. Valid values are "add", "preserve", and "fail". The default value is "add". | No
roundup | Whether the file modification times will be rounded up to the next even number of seconds. Zip archives store file modification times with a granularity of two seconds, so the times will either be rounded up or down. If you round down, the archive will always seem out-of-date when you rerun the task, so the default is to round up. Rounding up may lead to a different type of problems like JSPs inside a web archive that seem to be slightly more recent than precompiled pages, rendering precompilation useless. Defaults to true. Since Ant 1.6.2 | No
level | Non-default level at which file compression should be performed. Valid values range from 0 (no compression/fastest) to 9 (maximum compression/slowest). Since Ant 1.7 | No
preserve0permissions | when updating an archive or adding entries from a different archive Ant will assume that a Unix permissions value of 0 (nobody is allowed to do anything to the file/directory) means that the permissions haven't been stored at all rather than real permissions and will instead apply its own default values. Set this attribute to true if you really want to preserve the original permission field.since Ant 1.8.0 | No, default is false
useLanguageEncodingFlag | Whether to set the language encoding flag if the encoding is UTF-8. This setting doesn't have any effect if the encoding is not UTF-8. Since Ant 1.8.0. See also the discussion in the zip task page | No, default is true
createUnicodeExtraFields | Whether to create unicode extra fields to store the file names a second time inside the entry's metadata. Possible values are "never", "always" and "not-encodable" which will only add Unicode extra fields if the file name cannot be encoded using the specified encoding. Since Ant 1.8.0. See also the discussion in the zip task page | No, default is "never"
fallbacktoUTF8 | Whether to use UTF-8 and the language encoding flag instead of the specified encoding if a file name cannot be encoded using the specified encoding. Since Ant 1.8.0. See also the discussion in the zip task page | No, default is false
mergeClassPathAttributes | Whether to merge the Class-Path attributes found in different manifests (if merging manifests). If false, only the attribute of the last merged manifest will be preserved. Since Ant 1.8.0. unless you also set flattenAttributes to true this may result in manifests containing multiple Class-Path attributes which violates the manifest specification. | No, default is false
flattenAttributes | Whether to merge attributes occuring more than once in a section (this can only happen for the Class-Path attribute) into a single attribute. Since Ant 1.8.0. | No, default is false

### Nested elements

#### lib

The nested `lib` element specifies a FileSet. All files included in this fileset will end up in the `WEB-INF/lib` directory of the war file.

#### classes

The nested `classes` element specifies a FileSet. All files included in this fileset will end up in the `WEB-INF/classes` directory of the war file.

#### webinf

The nested `webinf` element specifies a FileSet. All files included in this fileset will end up in the `WEB-INF` directory of the war file. If this fileset includes a file named `web.xml`, the file is ignored and you will get a warning.

#### metainf

The nested `metainf` element specifies a FileSet. All files included in this fileset will end up in the `META-INF` directory of the war file. If this fileset includes a file named `MANIFEST.MF`, the file is ignored and you will get a warning.

#### manifest, indexjars, service

These are inherited from <jar>

### Examples

Assume the following structure in the project's base directory:

    thirdparty/libs/jdbc1.jar
    thirdparty/libs/jdbc2.jar
    build/main/com/myco/myapp/Servlet.class
    src/metadata/myapp.xml
    src/html/myapp/index.html
    src/jsp/myapp/front.jsp
    src/graphics/images/gifs/small/logo.gif
    src/graphics/images/gifs/large/logo.gif

then the war file `myapp.war` created with

    <war destfile="myapp.war" webxml="src/metadata/myapp.xml">
      <fileset dir="src/html/myapp"/>
      <fileset dir="src/jsp/myapp"/>
      <lib dir="thirdparty/libs">
        <exclude name="jdbc1.jar"/>
      </lib>
      <classes dir="build/main"/>
      <zipfileset dir="src/graphics/images/gifs"
                  prefix="images"/>
    </war>

will consist of

    WEB-INF/web.xml
    WEB-INF/lib/jdbc2.jar
    WEB-INF/classes/com/myco/myapp/Servlet.class
    META-INF/MANIFEST.MF
    index.html
    front.jsp
    images/small/logo.gif
    images/large/logo.gif

using Ant's default manifest file. The content of `WEB-INF/web.xml` is identical to `src/metadata/myapp.xml`.

We regulary receive bug reports that this task is creating the WEB-INF directory as web-inf (all lower case), and thus it is our fault your webapp doesn't work. The cause of these complaints lies in WinZip, which turns an all upper-case directory into an all lower case one in a fit of helpfulness. Please check that `jar xvf yourwebapp.war` shows the same behaviour before filing another report.
Winzip has an option allowing all uppercase names (which is off by default!). It can be enabled by: Menu "Options" -> "Configuration", "View" property/tab page, then "General" group box has an option called "Allow all uppercase file names".