bodewig 2003/02/05 08:16:28
Modified: src/main/org/apache/tools/ant/taskdefs Jar.java Zip.java
Log:
Remove the ugly hack, but still ensure that jars always at least contain a
manifest
Revision Changes Path
1.64 +46 -24
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.63
retrieving revision 1.64
diff -u -r1.63 -r1.64
--- Jar.java 5 Feb 2003 15:31:32 -0000 1.63
+++ Jar.java 5 Feb 2003 16:16:27 -0000 1.64
@@ -57,6 +57,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
@@ -135,6 +136,12 @@
/** jar index is JDK 1.3+ only */
private boolean index = false;
+ /**
+ * whether to really create the archive in createEmptyZip, will
+ * get set in getResourcesToAdd.
+ */
+ private boolean createEmpty = false;
+
/** constructor */
public Jar() {
super();
@@ -232,6 +239,11 @@
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 {
ZipFile zf = null;
try {
@@ -563,33 +575,43 @@
// no existing archive
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;
}
1.92 +27 -1
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.91
retrieving revision 1.92
diff -u -r1.91 -r1.92
--- Zip.java 5 Feb 2003 10:43:08 -0000 1.91
+++ Zip.java 5 Feb 2003 16:16:27 -0000 1.92
@@ -174,6 +174,14 @@
this.zipFile = destFile;
}
+ /**
+ * The file to create.
+ * @since Ant 1.5.2
+ */
+ public File getDestFile() {
+ return zipFile;
+ }
+
/**
* Directory from which to archive files; optional.
@@ -191,6 +199,15 @@
}
/**
+ * 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;
* optional, defaults to false.
*/
@@ -284,6 +301,15 @@
}
/**
+ * Encoding to use for filenames.
+ *
+ * @since Ant 1.5.2
+ */
+ public String getEncoding() {
+ return encoding;
+ }
+
+ /**
* validate and build
*/
public void execute() throws BuildException {
@@ -597,7 +623,7 @@
*
* @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
// because it does not permit a zero-entry archive.
// Must create it manually.