org.apache.tools.ant.taskdefs

Class Jar

public class Jar extends Zip

Creates a JAR archive.

Since: Ant 1.1

UNKNOWN: category="packaging"

Nested Class Summary
static classJar.FilesetManifestConfig
The manifest config enumerated type.
Field Summary
protected StringemptyBehavior
Constructor Summary
Jar()
constructor
Method Summary
voidaddConfiguredIndexJars(Path p)
Add a path to index jars.
voidaddConfiguredManifest(Manifest newManifest)
Allows the manifest for the archive file to be provided inline in the build file rather than in an external file.
voidaddConfiguredService(Service service)
A nested SPI service element.
voidaddMetainf(ZipFileSet fs)
Adds a zipfileset to include in the META-INF directory.
protected voidcleanUp()
Make sure we don't think we already have a MANIFEST next time this task gets executed.
protected booleancreateEmptyZip(File zipFile)
Create an empty jar file.
protected voidfinalizeZipOutputStream(ZipOutputStream zOut)
Finalize the zip output stream.
protected static StringfindJarName(String fileName, String[] classpath)
try to guess the name of the given file.
protected ArchiveStategetResourcesToAdd(ResourceCollection[] rcs, File zipFile, boolean needsUpdate)
Collect the resources that are newer than the corresponding entries (or missing) in the original archive.
protected static voidgrabFilesAndDirs(String file, List dirs, List files)
Grab lists of all root-level files and all directories contained in the given archive.
protected voidinitZipOutputStream(ZipOutputStream zOut)
Initialize the zip output stream.
voidreset()
reset to default values.
voidsetFilesetmanifest(Jar.FilesetManifestConfig config)
Behavior when a Manifest is found in a zipfileset or zipgroupfileset file.
voidsetIndex(boolean flag)
Set whether or not to create an index list for classes.
voidsetJarfile(File jarFile)
Set the destination file.
voidsetManifest(File manifestFile)
The manifest file to use.
voidsetManifestEncoding(String manifestEncoding)
The character encoding to use in the manifest file.
voidsetWhenempty(WhenEmpty we)
Not used for jar files.
voidsetWhenmanifestonly(WhenEmpty we)
Indicates if a jar file should be created when it would only contain a manifest file.
protected voidwriteIndexLikeList(List dirs, List files, PrintWriter writer)
Writes the directory entries from the first and the filenames from the second list to the given writer, one entry per line.
protected voidzipFile(InputStream is, ZipOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode)
Overridden from Zip class to deal with manifests and index lists.

Field Detail

emptyBehavior

protected String emptyBehavior

Constructor Detail

Jar

public Jar()
constructor

Method Detail

addConfiguredIndexJars

public void addConfiguredIndexJars(Path p)
Add a path to index jars.

Parameters: p a path

Since: Ant 1.6.2

addConfiguredManifest

public void addConfiguredManifest(Manifest newManifest)
Allows the manifest for the archive file to be provided inline in the build file rather than in an external file.

Parameters: newManifest an embedded manifest element

Throws: ManifestException on error

addConfiguredService

public void addConfiguredService(Service service)
A nested SPI service element.

Parameters: service the nested element.

Since: Ant 1.7

addMetainf

public void addMetainf(ZipFileSet fs)
Adds a zipfileset to include in the META-INF directory.

Parameters: fs zipfileset to add

cleanUp

protected void cleanUp()
Make sure we don't think we already have a MANIFEST next time this task gets executed.

See Also: Zip

createEmptyZip

protected boolean createEmptyZip(File zipFile)
Create an empty jar file.

Parameters: zipFile the file to create

Returns: true for historic reasons

Throws: BuildException on error

finalizeZipOutputStream

protected void finalizeZipOutputStream(ZipOutputStream zOut)
Finalize the zip output stream. This creates an index list if the index attribute is true.

Parameters: zOut the zip output stream

Throws: IOException on I/O errors BuildException on other errors

findJarName

protected static final String findJarName(String fileName, String[] classpath)
try to guess the name of the given file.

If this jar has a classpath attribute in its manifest, we can assume that it will only require an index of jars listed there. try to find which classpath entry is most likely the one the given file name points to.

In the absence of a classpath attribute, assume the other files will be placed inside the same directory as this jar and use their basename.

if there is a classpath and the given file doesn't match any of its entries, return null.

Parameters: fileName the name to look for classpath the classpath to look in (may be null)

Returns: the matching entry, or null if the file is not found

Since: Ant 1.6.2

getResourcesToAdd

protected ArchiveState getResourcesToAdd(ResourceCollection[] rcs, File zipFile, boolean needsUpdate)
Collect the resources that are newer than the corresponding entries (or missing) in the original archive.

If we are going to recreate the archive instead of updating it, all resources should be considered as new, if a single one is. Because of this, subclasses overriding this method must call super.getResourcesToAdd and indicate with the third arg if they already know that the archive is out-of-date.

Parameters: rcs The resource collections to grab resources from zipFile intended archive file (may or may not exist) needsUpdate whether we already know that the archive is out-of-date. Subclasses overriding this method are supposed to set this value correctly in their call to super.getResourcesToAdd.

Returns: an array of resources to add for each fileset passed in as well as a flag that indicates whether the archive is uptodate.

Throws: BuildException if it likes

grabFilesAndDirs

protected static final void grabFilesAndDirs(String file, List dirs, List files)
Grab lists of all root-level files and all directories contained in the given archive.

Parameters: file the zip file to examine dirs where to place the directories found files where to place the files found

Throws: IOException on error

Since: Ant 1.7

initZipOutputStream

protected void initZipOutputStream(ZipOutputStream zOut)
Initialize the zip output stream.

Parameters: zOut the zip output stream

Throws: IOException on I/O errors BuildException on other errors

reset

public void reset()
reset to default values.

Since: 1.44, Ant 1.5

See Also: Zip

setFilesetmanifest

public void setFilesetmanifest(Jar.FilesetManifestConfig config)
Behavior when a Manifest is found in a zipfileset or zipgroupfileset file. Valid values are "skip", "merge", and "mergewithoutmain". "merge" will merge all of manifests together, and merge this into any other specified manifests. "mergewithoutmain" merges everything but the Main section of the manifests. Default value is "skip". Note: if this attribute's value is not "skip", the created jar will not be readable by using java.util.jar.JarInputStream

Parameters: config setting for found manifest behavior.

setIndex

public void setIndex(boolean flag)
Set whether or not to create an index list for classes. This may speed up classloading in some cases.

Parameters: flag a boolean value

setJarfile

public void setJarfile(File jarFile)

Deprecated: since 1.5.x. Use setDestFile(File) instead.

Set the destination file.

Parameters: jarFile the destination file

setManifest

public void setManifest(File manifestFile)
The manifest file to use. This can be either the location of a manifest, or the name of a jar added through a fileset. If its the name of an added jar, the task expects the manifest to be in the jar at META-INF/MANIFEST.MF.

Parameters: manifestFile the manifest file to use.

setManifestEncoding

public void setManifestEncoding(String manifestEncoding)
The character encoding to use in the manifest file.

Parameters: manifestEncoding the character encoding

setWhenempty

public void setWhenempty(WhenEmpty we)
Not used for jar files.

Parameters: we not used

UNKNOWN: ignore="true"

setWhenmanifestonly

public void setWhenmanifestonly(WhenEmpty we)
Indicates if a jar file should be created when it would only contain a manifest file. Possible values are: fail (throw an exception and halt the build); skip (do not create any archive, but issue a warning); create (make an archive with only a manifest file). Default is create;

Parameters: we a WhenEmpty enumerated value

writeIndexLikeList

protected final void writeIndexLikeList(List dirs, List files, PrintWriter writer)
Writes the directory entries from the first and the filenames from the second list to the given writer, one entry per line.

Parameters: dirs a list of directories files a list of files writer the writer to write to

Throws: IOException on error

Since: Ant 1.6.2

zipFile

protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode)
Overridden from Zip class to deal with manifests and index lists.

Parameters: is the input stream zOut the zip output stream vPath the name this entry shall have in the archive lastModified last modification time for the entry. fromArchive the original archive we are copying this entry from, will be null if we are not copying from an archive. mode the Unix permissions to set.

Throws: IOException on error