# Ant User Manual

## Jar

### Description

Jars a set of files.

The basedir attribute is the reference directory from where to jar.

Note that file permissions will not be stored in the resulting jarfile.

It is possible to refine the set of files that are being jarred. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes. With the includes or includesfile attribute you specify the files you want to have included by using patterns. The exclude or excludesfile attribute is used to specify the files you want to have excluded. This is also done with patterns. And finally with the defaultexcludes attribute, you can specify whether you want to use default exclusions or not. See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.

This task forms an implicit FileSet and supports all attributes of `<fileset>` (`dir` becomes `basedir`) as well as the nested `<include>`, `<exclude>` and `<patternset>` elements.

You can also use nested file sets for more flexibility, and specify multiple ones to merge together different trees of files into one JAR. The extended fileset attributes from the zip task are also available in the jar task. See the Zip task for more details and examples.

If the manifest is omitted, a simple one will be supplied by Ant. You should not include `META-INF/MANIFEST.MF` in your set of files.

The `whenempty` parameter controls what happens when no files match. If `create` (the default), the JAR is created anyway with only a manifest. If `skip`, the JAR is not created and a warning is issued. If `fail`, the JAR is not created and the build is halted with an error.

(The Jar task is a shortcut for specifying the manifest file of a JAR file. The same thing can be accomplished by using the fullpath attribute of a zipfileset in a Zip task. The one difference is that if the manifest attribute is not specified, the Jar task will include an empty one for you.)

### Parameters

Attribute | Description | Required
jarfile | the jar-file to create. | Yes
basedir | the directory from which to jar the files. | No
compress | Not only store data but also compress them, defaults to true | No
includes | comma 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 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
whenempty | Behavior to use if no files match. | No

### Examples

      <jar jarfile="${dist}/lib/app.jar" basedir="${build}/classes"/>

jars all files in the `${build}/classes` directory into a file called `app.jar` in the `${dist}/lib` directory.

      <jar jarfile="${dist}/lib/app.jar"
           basedir="${build}/classes"
           excludes="**/Test.class"
      />

jars all files in the `${build}/classes` directory into a file called `app.jar` in the `${dist}/lib` directory. Files with the name `Test.class` are excluded.

      <jar jarfile="${dist}/lib/app.jar"
           basedir="${build}/classes"
           includes="mypackage/test/**"
           excludes="**/Test.class"
      />

jars all files in the `${build}/classes` directory into a file called `app.jar` in the `${dist}/lib` directory. Only files under the directory `mypackage/test` are used, and files with the name `Test.class` are excluded.

      <jar jarfile="${dist}/lib/app.jar">
        <fileset dir="${build}/classes"
                 excludes="**/Test.class"
        />
        <fileset dir="${src}/resources"/>
      </jar>

jars all files in the `${build}/classes` directory and also in the `${src}/resources` directory together into a file called `app.jar` in the `${dist}/lib` directory. Files with the name `Test.class` are excluded. If there are files such as `${build}/classes/mypackage/MyClass.class` and `${src}/resources/mypackage/image.gif`, they will appear in the same directory in the JAR (and thus be considered in the same package by Java).

Copyright © 2000,2001 Apache Software Foundation. All rights Reserved.