<pack> is a task which, given a set C of compiled Java™ classes, computes the support set for C - that is, the set of classes which the classes in C depend upon - and create a JAR containing all and only the classes needed to run C.

In other words, it builds the smallest JAR possible to link and run any given class.

Additional features comprise the definition of a Main-Class and a Class-Path for the JAR, plus the possibility of explicitly adding additional classes (and their support sets) and files.

Note: <pack> analyzes classfiles to discover dependencies; it cannot, however, detect classes which are linked or loaded indirectly (via Class.forName or similar mechanism), as such classes can even be completely undetermined until runtime (imagine some code that obtains a class name from the user, and attempts to load the class via Class.forName).

However, if such classes are known when the packing occurs, the "additionalClass" subelement of <pack> can be used to explicitly include them in the produced JAR while visually distinguishing them from the core packed classes.

Similarly, the "ignoreClass" elements can be used to explicitly ignore classes.

2. Defining the task in an Ant build script (typically build.xml)

As any external Ant task, <pack> needs to be defined in your build script. <pack> lives in the org.sadun.util.ant package, which in turn is contained in the org.sadun.util.jar library, and its class name is Pack. Therefore, add an entry

in your <project> (where classpath is the path to the org.sadun.util.jar library) to be able to use <pack>.

3. Task attributes and subelements

The following are attributes of the <pack> element.

Attribute

Type

Description

classes

(mandatory (*))

A comma-separated list of classes to pack.

packages

(mandatory (*))

A comma-separated list of packages to pack.

classpath

(optional)

Classpath to use when packing classes. A nested standard Ant <classpath> element can be also used (see Ant documentation for details).
The classpath is initialized according to the value of build.sysclasspath (which in turn is valued "first" by default, for backwards compatibility).

targetJar

(mandatory)

The name of the jar file to produce.

mainfestClassPath

(optional)

The manifest Class-Path entry.

mainfestMainClass

(optional)

The manifest Main-Class entry.

excludePkg

(optional)

A comma-separated list of package prefixes to exclude. Defaults to java,javax,sun.

includePkg

(optional)

A comma-separated list of package prefixes to include. Only classes in matching packages will be included. Has lower precedence than excludePkg.

resolveFiltered

(optional)

If true, allows resolution of classes which are filtered out. Defaults to false

cacheClassFiles

(optional)

If false, disables classfile caching, which slows down the packing process but saves memory. Defaults to true

(*) classes and packages are alternative.

The classes specified by classes and all the classes in their supporting set must be in Ant classpath or in the classpath specified by the optional classpath attribute. In other words, if your classes use some libraries, etc, they must be in classpath.

The following definition will create a runnable JAR as above, but adding the entire library to the JAR and adding the relative Class-Path entry to the Manifest. Only the classes whose fully qualified name is prefixed with my.test are included in the resulting jar:

The following definition will create a runnable JAR with all the classes and necessary for linking the entire package my.test.pkg which makes use of the my.library.jar library (see also the notes above):