Browse Source

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 <don@bea.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268458 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 24 years ago
parent
commit
54b84962a5
3 changed files with 53 additions and 54 deletions
  1. +4
    -3
      src/main/org/apache/tools/ant/taskdefs/Jar.java
  2. +10
    -10
      src/main/org/apache/tools/ant/taskdefs/War.java
  3. +39
    -41
      src/main/org/apache/tools/ant/taskdefs/Zip.java

+ 4
- 3
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -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 @@ public class Jar extends Zip {
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);
}




+ 10
- 10
src/main/org/apache/tools/ant/taskdefs/War.java View File

@@ -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 @@ public class War extends Jar {
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)


+ 39
- 41
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -109,40 +109,18 @@ public class Zip extends MatchingTask {
/**
* 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.
@@ -164,7 +142,7 @@ public class Zip extends MatchingTask {
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 @@ public class Zip extends MatchingTask {

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 @@ public class Zip extends MatchingTask {
}
}

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 @@ public class Zip extends MatchingTask {
}

/**
* 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 @@ public class Zip extends MatchingTask {
} 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);
}
}
}



Loading…
Cancel
Save