donaldp 01/01/16 05:16:47
Modified: src/main/org/apache/tools/ant/taskdefs Jar.java War.java
Zip.java
Log:
Addition of ZipFileset facilities. Descibed by the author
---
With these patches, Zip (and derivative tasks such as Jar and War) can
merge the entries of multiple zip files into a single output zip file.
The contents of an input zip file may be selectively extracted based on
include/exclude patterns.
An included zip file is specified using a <fileset> with a "src" attribute,
as in:
<target name="jartest">
<jar jarfile="utils.jar">
<fileset
src="weblogic.jar"
includes="weblogic/utils/"
excludes="weblogic/utils/jars/,**/reflect/"
/>
</jar>
</target>
In this example, a subset of the "weblogic/utils" directory is extracted
from weblogic.jar, into utils.jar.
The fileset may also contain "prefix" and "fullpath" attributes (the
functionality of PrefixedFileSet has been retained in the new class
ZipFileSet). Prefixes apply to directory-based and zip-based filesets.
The fullpath attributes applies only to a single file in a directory-based
fileset.
The War task may extract entries from a zip file for all of its filesets
(including the files in "classes" and "lib").
The motivation for this change is:
1) There is significant overlap between "jlink" and "zip", and it seemed
better to combine them.
2) "jlink" does not support include/exclude patterns which are extremely
useful for writing packaging-type tasks such as Zip/Jar/War. This
was my main motivation.
3) By adding this functionality to the base task, it can also be used in
derivative tasks such as Jar and War.
---
Submitted By: Don Ferguson <[EMAIL PROTECTED]>
Revision Changes Path
1.15 +4 -3
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java
Index: Jar.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- Jar.java 2001/01/08 16:45:32 1.14
+++ Jar.java 2001/01/16 13:16:46 1.15
@@ -55,6 +55,7 @@
package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*;
+import org.apache.tools.ant.types.ZipFileSet;
import java.io.*;
import java.util.zip.*;
@@ -85,12 +86,12 @@
if (!manifest.exists())
throw new BuildException("Manifest file: " + manifest + " does
not exist.");
- // Create a PrefixedFileSet for this file, and pass it up.
- PrefixedFileSet fs = new PrefixedFileSet();
+ // Create a ZipFileSet for this file, and pass it up.
+ ZipFileSet fs = new ZipFileSet();
fs.setDir(new File(manifest.getParent()));
fs.setIncludes(manifest.getName());
fs.setFullpath("META-INF/MANIFEST.MF");
- super.addPrefixedfileset(fs);
+ super.addFileset(fs);
}
1.10 +10 -10
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/War.java
Index: War.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/War.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- War.java 2001/01/08 16:45:32 1.9
+++ War.java 2001/01/16 13:16:46 1.10
@@ -55,7 +55,7 @@
package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*;
-import org.apache.tools.ant.types.FileSet;
+import org.apache.tools.ant.types.ZipFileSet;
import java.io.*;
import java.util.Vector;
@@ -86,30 +86,30 @@
if (!deploymentDescriptor.exists())
throw new BuildException("Deployment descriptor: " +
deploymentDescriptor + " does not exist.");
- // Create a PrefixedFileSet for this file, and pass it up.
- PrefixedFileSet fs = new PrefixedFileSet();
+ // Create a ZipFileSet for this file, and pass it up.
+ ZipFileSet fs = new ZipFileSet();
fs.setDir(new File(deploymentDescriptor.getParent()));
fs.setIncludes(deploymentDescriptor.getName());
fs.setFullpath("WEB-INF/web.xml");
- super.addPrefixedfileset(fs);
+ super.addFileset(fs);
}
- public void addLib(PrefixedFileSet fs) {
+ public void addLib(ZipFileSet fs) {
// We just set the prefix for this fileset, and pass it up.
fs.setPrefix("WEB-INF/lib/");
- super.addPrefixedfileset(fs);
+ super.addFileset(fs);
}
- public void addClasses(PrefixedFileSet fs) {
+ public void addClasses(ZipFileSet fs) {
// We just set the prefix for this fileset, and pass it up.
fs.setPrefix("WEB-INF/classes/");
- super.addPrefixedfileset(fs);
+ super.addFileset(fs);
}
- public void addWebinf(PrefixedFileSet fs) {
+ public void addWebinf(ZipFileSet fs) {
// We just set the prefix for this fileset, and pass it up.
fs.setPrefix("WEB-INF/");
- super.addPrefixedfileset(fs);
+ super.addFileset(fs);
}
protected void initZipOutputStream(ZipOutputStream zOut)
1.27 +39 -41
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java
Index: Zip.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- Zip.java 2001/01/08 16:45:32 1.26
+++ Zip.java 2001/01/16 13:16:46 1.27
@@ -109,41 +109,19 @@
/**
* Adds a set of files (nested fileset attribute).
*/
- public void addFileset(FileSet set) {
+ public void addFileset(ZipFileSet set) {
filesets.addElement(set);
}
/**
- * Adds a set of files (nested fileset attribute).
+ * @deprecated addPrefixedfileset is deprecated; replaced by ZipFileSet
*/
- public void addPrefixedfileset(PrefixedFileSet set) {
- addFileset(set);
+ public void addPrefixedfileset(ZipFileSet set) {
+ log("WARNING: PrefixedFileSets are deprecated; use the fileset tag
instead.");
+ filesets.addElement(set);
}
- /**
- * FileSet with an additional prefix attribute to specify the
- * location we want to move the files to (inside the archive).
- * Or, if this FileSet represents only a single file, then the
- * fullpath attribute can be set, which specifies the full path
- * that the file should have when it is placed in the archive.
- */
- public static class PrefixedFileSet extends FileSet {
- private String prefix = "";
- private String fullpath = "";
-
- public void setPrefix(String loc) {
- prefix = loc;
- }
-
- public String getPrefix() {return prefix;}
- public void setFullpath(String loc) {
- fullpath = loc;
- }
-
- public String getFullpath() {return fullpath;}
- }
-
/**
* Sets behavior of the task when no files match.
* Possible values are: <code>fail</code> (throw an exception
@@ -164,7 +142,7 @@
public void execute() throws BuildException {
if (baseDir == null && filesets.size() == 0 &&
"zip".equals(archiveType)) {
throw new BuildException( "basedir attribute must be set, or at
least " +
- "one fileset or prefixedfileset must
be given!" );
+ "one fileset must be given!" );
}
if (zipFile == null) {
@@ -191,7 +169,8 @@
try {
boolean success = false;
- ZipOutputStream zOut = new ZipOutputStream(new
FileOutputStream(zipFile));
+ ZipOutputStream zOut =
+ new ZipOutputStream(new FileOutputStream(zipFile));
try {
if (doCompress) {
zOut.setMethod(ZipOutputStream.DEFLATED);
@@ -283,6 +262,26 @@
}
}
+ protected void addZipEntries(ZipFileSet fs, DirectoryScanner ds,
+ ZipOutputStream zOut, String prefix)
+ throws IOException
+ {
+ ZipScanner zipScanner = (ZipScanner) ds;
+ String zipSrc = fs.getSrc();
+
+ ZipEntry entry;
+ ZipInputStream in = new ZipInputStream(new FileInputStream(new
File(zipSrc)));
+ while ((entry = in.getNextEntry()) != null) {
+ String vPath = entry.getName();
+ if (zipScanner.match(vPath)) {
+ addParentDirs(null, vPath, zOut, prefix);
+ if (! entry.isDirectory()) {
+ zipFile(in, zOut, prefix+vPath, entry.getTime());
+ }
+ }
+ }
+ }
+
protected void initZipOutputStream(ZipOutputStream zOut)
throws IOException, BuildException
{
@@ -499,28 +498,22 @@
}
/**
- * Iterate over the given Vector of (prefixed)filesets and add
+ * Iterate over the given Vector of zipfilesets and add
* all files to the ZipOutputStream using the given prefix.
*/
protected void addFiles(Vector filesets, ZipOutputStream zOut)
throws IOException {
// Add each fileset in the Vector.
for (int i = 0; i<filesets.size(); i++) {
- FileSet fs = (FileSet) filesets.elementAt(i);
+ ZipFileSet fs = (ZipFileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(project);
- String prefix = "";
- String fullpath = "";
- if (fs instanceof PrefixedFileSet) {
- PrefixedFileSet pfs = (PrefixedFileSet) fs;
- prefix = pfs.getPrefix();
- fullpath = pfs.getFullpath();
- }
-
+ String prefix = fs.getPrefix();
if (prefix.length() > 0
&& !prefix.endsWith("/")
&& !prefix.endsWith("\\")) {
prefix += "/";
}
+ String fullpath = fs.getFullpath();
// Need to manually add either fullpath's parent directory, or
// the prefix directory, to the archive.
if (prefix.length() > 0) {
@@ -529,8 +522,13 @@
} else if (fullpath.length() > 0) {
addParentDirs(null, fullpath, zOut, "");
}
- // Add the fileset.
- addFiles(ds, zOut, prefix, fullpath);
+
+ if (fs.getSrc() != null) {
+ addZipEntries(fs, ds, zOut, prefix);
+ } else {
+ // Add the fileset.
+ addFiles(ds, zOut, prefix, fullpath);
+ }
}
}