org.codehaus.plexus.archiver.jar
Class JarArchiver

java.lang.Object
  extended by org.codehaus.plexus.logging.AbstractLogEnabled
      extended by org.codehaus.plexus.archiver.AbstractArchiver
          extended by org.codehaus.plexus.archiver.zip.AbstractZipArchiver
              extended by org.codehaus.plexus.archiver.zip.ZipArchiver
                  extended by org.codehaus.plexus.archiver.jar.JarArchiver
All Implemented Interfaces:
Archiver, FilterEnabled, FinalizerEnabled, org.codehaus.plexus.logging.LogEnabled, org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable
Direct Known Subclasses:
EarArchiver, WarArchiver

public class JarArchiver
extends ZipArchiver

Base class for tasks that build archives in JAR file format.

Version:
$Revision: 3560 $ $Date: 2006-07-25 01:14:00 -0400 (Tue, 25 Jul 2006) $

Nested Class Summary
static class JarArchiver.FilesetManifestConfig
           
 
Field Summary
private  Manifest configuredManifest
          merged manifests added through addConfiguredManifest
private  boolean createEmpty
          whether to really create the archive in createEmptyZip, will get set in getResourcesToAdd.
private  Manifest filesetManifest
          merged manifests added through filesets
private  JarArchiver.FilesetManifestConfig filesetManifestConfig
          whether to merge fileset manifests; value is true if filesetmanifest is 'merge' or 'mergewithoutmain'
private  boolean index
          jar index is JDK 1.3+ only
private static java.lang.String INDEX_NAME
          The index file name.
private  java.util.ArrayList indexJars
          Path containing jars that shall be indexed in addition to this archive.
private  Manifest manifest
          the manifest specified by the 'manifest' attribute *
private static java.lang.String MANIFEST_NAME
          The manifest file name.
private  java.lang.String manifestEncoding
          The encoding to use when reading in a manifest file
private  java.io.File manifestFile
          The file found from the 'manifest' attribute.
private  boolean mergeManifestsMain
          whether to merge the main section of fileset manifests; value is true if filesetmanifest is 'merge'
private  Manifest originalManifest
          Manifest of original archive, will be set to null if not in update mode.
private  java.util.Vector rootEntries
          Stores all files that are in the root of the archive (i.e.
private  Manifest savedConfiguredManifest
          shadow of the above if upToDate check alters the value
 
Fields inherited from class org.codehaus.plexus.archiver.zip.AbstractZipArchiver
addedDirs, addingNewFiles, archiveType, doubleFilePass, duplicate, entries, skipWriting
 
Fields inherited from class org.codehaus.plexus.archiver.AbstractArchiver
DEFAULT_DIR_MODE, DEFAULT_FILE_MODE
 
Fields inherited from interface org.codehaus.plexus.archiver.Archiver
ROLE
 
Constructor Summary
JarArchiver()
          constructor
 
Method Summary
 void addConfiguredIndexJars(java.io.File indexJar)
           
 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.
protected  void cleanUp()
          Make sure we don't think we already have a MANIFEST next time this task gets executed.
protected  boolean createEmptyZip(java.io.File zipFile)
          Create an empty zip file
private  void createIndexList(ZipOutputStream zOut)
          Create the index list to speed up classloading.
private  Manifest createManifest()
           
private  void filesetManifest(java.io.File file, java.io.InputStream is)
           
protected  void finalizeZipOutputStream(ZipOutputStream zOut)
          method for subclasses to override
protected static java.lang.String findJarName(java.lang.String fileName, java.lang.String[] classpath)
          try to guess the name of the given file.
private  Manifest getManifest(java.io.File manifestFile)
           
private  Manifest getManifest(java.io.Reader r)
           
protected static void grabFilesAndDirs(java.lang.String file, java.util.List dirs, java.util.List files)
          Grab lists of all root-level files and all directories contained in the given archive.
protected  void initZipOutputStream(ZipOutputStream zOut)
          method for subclasses to override
 void reset()
          reset to default values.
 void setFilesetmanifest(JarArchiver.FilesetManifestConfig config)
          Behavior when a Manifest is found in a zipfileset or zipgroupfileset file.
 void setIndex(boolean flag)
          Set whether or not to create an index list for classes.
 void setManifest(java.io.File manifestFile)
          The manifest file to use.
 void setManifestEncoding(java.lang.String manifestEncoding)
          Set whether or not to create an index list for classes.
protected  void writeIndexLikeList(java.util.List dirs, java.util.List files, java.io.PrintWriter writer)
          Writes the directory entries from the first and the filenames from the second list to the given writer, one entry per line.
private  void writeManifest(ZipOutputStream zOut, Manifest manifest)
           
protected  void zipFile(java.io.InputStream is, ZipOutputStream zOut, java.lang.String vPath, long lastModified, java.io.File fromArchive, int mode)
          Overridden from Zip class to deal with manifests and index lists.
 
Methods inherited from class org.codehaus.plexus.archiver.zip.AbstractZipArchiver
addParentDirs, addResources, createArchive, getArchiveFinalizers, getComment, getDirMode, getEncoding, getFileMode, getResourcesToAdd, getResourcesToUpdate, isCompress, isFilesonly, isInUpdateMode, isRoundUp, isSupportingForced, runArchiveFinalizers, setArchiveFilters, setArchiveFinalizers, setComment, setCompress, setDirMode, setEncoding, setFileMode, setFilesonly, setRoundUp, setUpdateMode, zipDir, zipFile
 
Methods inherited from class org.codehaus.plexus.archiver.AbstractArchiver
addArchivedFileSet, addArchivedFileSet, addArchivedFileSet, addArchivedFileSet, addDirectory, addDirectory, addDirectory, addDirectory, addFile, addFile, checkForced, contextualize, getDefaultDirectoryMode, getDefaultFileMode, getDestFile, getDirs, getFiles, getIncludeEmptyDirs, getLogger, isForced, isUptodate, setDefaultDirectoryMode, setDefaultFileMode, setDestFile, setForced, setIncludeEmptyDirs
 
Methods inherited from class org.codehaus.plexus.logging.AbstractLogEnabled
enableLogging, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INDEX_NAME

private static final java.lang.String INDEX_NAME
The index file name.

See Also:
Constant Field Values

MANIFEST_NAME

private static final java.lang.String MANIFEST_NAME
The manifest file name.

See Also:
Constant Field Values

configuredManifest

private Manifest configuredManifest
merged manifests added through addConfiguredManifest


savedConfiguredManifest

private Manifest savedConfiguredManifest
shadow of the above if upToDate check alters the value


filesetManifest

private Manifest filesetManifest
merged manifests added through filesets


originalManifest

private Manifest originalManifest
Manifest of original archive, will be set to null if not in update mode.


filesetManifestConfig

private JarArchiver.FilesetManifestConfig filesetManifestConfig
whether to merge fileset manifests; value is true if filesetmanifest is 'merge' or 'mergewithoutmain'


mergeManifestsMain

private boolean mergeManifestsMain
whether to merge the main section of fileset manifests; value is true if filesetmanifest is 'merge'


manifest

private Manifest manifest
the manifest specified by the 'manifest' attribute *


manifestEncoding

private java.lang.String manifestEncoding
The encoding to use when reading in a manifest file


manifestFile

private java.io.File manifestFile
The file found from the 'manifest' attribute. 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 manifest is looked for in META-INF/MANIFEST.MF


index

private boolean index
jar index is JDK 1.3+ only


createEmpty

private boolean createEmpty
whether to really create the archive in createEmptyZip, will get set in getResourcesToAdd.


rootEntries

private java.util.Vector rootEntries
Stores all files that are in the root of the archive (i.e. that have a name that doesn't contain a slash) so they can get listed in the index.

Will not be filled unless the user has asked for an index.


indexJars

private java.util.ArrayList indexJars
Path containing jars that shall be indexed in addition to this archive.

Constructor Detail

JarArchiver

public JarArchiver()
constructor

Method Detail

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.


setManifestEncoding

public void setManifestEncoding(java.lang.String manifestEncoding)
Set whether or not to create an index list for classes. This may speed up classloading in some cases.


addConfiguredManifest

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

Parameters:
newManifest -
Throws:
ManifestException

setManifest

public void setManifest(java.io.File manifestFile)
                 throws ArchiverException
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.
Throws:
ArchiverException

getManifest

private Manifest getManifest(java.io.File manifestFile)
                      throws ArchiverException
Throws:
ArchiverException

getManifest

private Manifest getManifest(java.io.Reader r)
                      throws ArchiverException
Throws:
ArchiverException

setFilesetmanifest

public void setFilesetmanifest(JarArchiver.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.

addConfiguredIndexJars

public void addConfiguredIndexJars(java.io.File indexJar)

initZipOutputStream

protected void initZipOutputStream(ZipOutputStream zOut)
                            throws java.io.IOException,
                                   ArchiverException
Description copied from class: AbstractZipArchiver
method for subclasses to override

Overrides:
initZipOutputStream in class AbstractZipArchiver
Throws:
java.io.IOException
ArchiverException

createManifest

private Manifest createManifest()
                         throws ArchiverException
Throws:
ArchiverException

writeManifest

private void writeManifest(ZipOutputStream zOut,
                           Manifest manifest)
                    throws java.io.IOException,
                           ArchiverException
Throws:
java.io.IOException
ArchiverException

finalizeZipOutputStream

protected void finalizeZipOutputStream(ZipOutputStream zOut)
                                throws java.io.IOException,
                                       ArchiverException
Description copied from class: AbstractZipArchiver
method for subclasses to override

Overrides:
finalizeZipOutputStream in class AbstractZipArchiver
Throws:
java.io.IOException
ArchiverException

createIndexList

private void createIndexList(ZipOutputStream zOut)
                      throws java.io.IOException,
                             ArchiverException
Create the index list to speed up classloading. This is a JDK 1.3+ specific feature and is enabled by default. See the JAR index specification for more details.

Parameters:
zOut - the zip stream representing the jar being built.
Throws:
java.io.IOException - thrown if there is an error while creating the index and adding it to the zip stream.
ArchiverException

zipFile

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

Overrides:
zipFile in class AbstractZipArchiver
Parameters:
is - the stream to read data for the entry from.
zOut - the stream to write to.
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:
java.io.IOException
ArchiverException

filesetManifest

private void filesetManifest(java.io.File file,
                             java.io.InputStream is)
                      throws ArchiverException
Throws:
ArchiverException

createEmptyZip

protected boolean createEmptyZip(java.io.File zipFile)
                          throws ArchiverException
Description copied from class: AbstractZipArchiver
Create an empty zip file

Overrides:
createEmptyZip in class AbstractZipArchiver
Returns:
true for historic reasons
Throws:
ArchiverException

cleanUp

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

Overrides:
cleanUp in class AbstractZipArchiver
See Also:
AbstractZipArchiver.cleanUp()

reset

public void reset()
reset to default values.

Overrides:
reset in class AbstractZipArchiver
See Also:
AbstractZipArchiver.reset()

writeIndexLikeList

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


findJarName

protected static final java.lang.String findJarName(java.lang.String fileName,
                                                    java.lang.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.


grabFilesAndDirs

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

Throws:
java.io.IOException