Browse Source

Remove the ugly hack, but still ensure that jars always at least contain a manifest

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273993 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
1cea69ac67
2 changed files with 73 additions and 25 deletions
  1. +46
    -24
      src/main/org/apache/tools/ant/taskdefs/Jar.java
  2. +27
    -1
      src/main/org/apache/tools/ant/taskdefs/Zip.java

+ 46
- 24
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -57,6 +57,7 @@ package org.apache.tools.ant.taskdefs;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -135,6 +136,12 @@ public class Jar extends Zip {
/** jar index is JDK 1.3+ only */ /** jar index is JDK 1.3+ only */
private boolean index = false; private boolean index = false;


/**
* whether to really create the archive in createEmptyZip, will
* get set in getResourcesToAdd.
*/
private boolean createEmpty = false;

/** constructor */ /** constructor */
public Jar() { public Jar() {
super(); super();
@@ -232,6 +239,11 @@ public class Jar extends Zip {
return newManifest; return newManifest;
} }


/**
* @return null if jarFile doesn't contain a manifest, the
* manifest otherwise.
* @since Ant 1.5.2
*/
private Manifest getManifestFromJar(File jarFile) throws IOException { private Manifest getManifestFromJar(File jarFile) throws IOException {
ZipFile zf = null; ZipFile zf = null;
try { try {
@@ -563,33 +575,43 @@ public class Jar extends Zip {
// no existing archive // no existing archive
needsUpdate = true; needsUpdate = true;
} }
Resource[][] fromZip =
super.getResourcesToAdd(filesets, zipFile, needsUpdate);
if (needsUpdate && isEmpty(fromZip)) {
// archive doesn't have any content apart from the manifest


/*
* OK, this is a hack.
*
* Zip doesn't care if the array we return is longer than
* the array of filesets, so we can savely append an
* additional non-empty array. This will make Zip think
* that there are resources out-of-date and at the same
* time add nothing.
*
* The whole manifest handling happens in initZipOutputStream.
*/
Resource[][] tmp = new Resource[fromZip.length + 1][];
System.arraycopy(fromZip, 0, tmp, 0, fromZip.length);
tmp[fromZip.length] = new Resource[] {new Resource("")};
fromZip = tmp;
}
return fromZip;
createEmpty = needsUpdate;
return super.getResourcesToAdd(filesets, zipFile, needsUpdate);
} }


protected boolean createEmptyZip(File zipFile) {
// Jar files always contain a manifest and can never be empty
protected boolean createEmptyZip(File zipFile) throws BuildException {
if (!createEmpty) {
return true;
}
ZipOutputStream zOut = null;
try {
log("Building jar: " + getDestFile().getAbsolutePath());
zOut = new ZipOutputStream(new FileOutputStream(getDestFile()));

zOut.setEncoding(getEncoding());
if (isCompress()) {
zOut.setMethod(ZipOutputStream.DEFLATED);
} else {
zOut.setMethod(ZipOutputStream.STORED);
}
initZipOutputStream(zOut);
finalizeZipOutputStream(zOut);
} catch (IOException ioe) {
throw new BuildException("Could not create almost empty JAR archive "
+ "(" + ioe.getMessage() + ")", ioe,
getLocation());
} finally {
// Close the output stream.
try {
if (zOut != null) {
zOut.close();
}
} catch (IOException ex) {
}
createEmpty = false;
}
return true; return true;
} }




+ 27
- 1
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -174,6 +174,14 @@ public class Zip extends MatchingTask {
this.zipFile = destFile; this.zipFile = destFile;
} }


/**
* The file to create.
* @since Ant 1.5.2
*/
public File getDestFile() {
return zipFile;
}



/** /**
* Directory from which to archive files; optional. * Directory from which to archive files; optional.
@@ -190,6 +198,15 @@ public class Zip extends MatchingTask {
doCompress = c; doCompress = c;
} }


/**
* Whether we want to compress the files or only store them;
*
* @since Ant 1.5.2
*/
public boolean isCompress() {
return doCompress;
}

/** /**
* If true, emulate Sun's jar utility by not adding parent directories; * If true, emulate Sun's jar utility by not adding parent directories;
* optional, defaults to false. * optional, defaults to false.
@@ -283,6 +300,15 @@ public class Zip extends MatchingTask {
this.encoding = encoding; this.encoding = encoding;
} }


/**
* Encoding to use for filenames.
*
* @since Ant 1.5.2
*/
public String getEncoding() {
return encoding;
}

/** /**
* validate and build * validate and build
*/ */
@@ -597,7 +623,7 @@ public class Zip extends MatchingTask {
* *
* @return true for historic reasons * @return true for historic reasons
*/ */
protected boolean createEmptyZip(File zipFile) {
protected boolean createEmptyZip(File zipFile) throws BuildException {
// In this case using java.util.zip will not work // In this case using java.util.zip will not work
// because it does not permit a zero-entry archive. // because it does not permit a zero-entry archive.
// Must create it manually. // Must create it manually.


Loading…
Cancel
Save