3.14 Building JAR Files

3.14.1 Problem

You want to build a JAR file.

3.14.2 Solution

Use Ant's jar task.

3.14.3 Discussion

The jar task creates JAR files, as expected. In its simplest form, you specify the name of the new JAR file along with the directory containing files to add to the archive. All files in the directory specified by basedir along with subdirectories and files are added:

<jar jarfile="${dir.dist}/oreilly.jar" 

The jar task tries to be as efficient as possible. Before creating a new JAR, it checks for an existing archive and compares file timestamps. In our example, oreilly.jar is only created if it does not exist, or if any of the files in ${dir.build} are newer than oreilly.jar.

This next example refines our operation by only including .class files, unless they are unit tests matching the Test*.class pattern:

<jar jarfile="${dir.dist}/oreilly.jar" 

The includes and excludes attributes use the ** pattern to represent any subdirectory. Use nested <fileset> tags for more sophisticated selections:

<jar jarfile="${dir.dist}/oreilly.jar">
  <fileset dir="${dir.build}" 
  <fileset dir="${dir.src}"

This JAR file consists of all .class files (except for Test*.class) in the build directory tree. It also contains all .properties files under the source directory tree.

Ant makes it completely trivial to exclude test cases from a production JAR fileliterally, less than one line of text in the build file. Some teams make their lives quite hard by having a separate source tree for their test classes, enduring all kinds of IDE gymnastics because they are mortified they might inflate their production JARs with test cases.

3.14.4 See Also

Recipe 3.10 covers Ant's pattern-matching syntax.