From 1cea69ac674b7852c3e00a255bd2aec352218695 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 5 Feb 2003 16:16:27 +0000 Subject: [PATCH] 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 --- .../org/apache/tools/ant/taskdefs/Jar.java | 70 ++++++++++++------- .../org/apache/tools/ant/taskdefs/Zip.java | 28 +++++++- 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index 558a808ce..1e1049e2e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -57,6 +57,7 @@ package org.apache.tools.ant.taskdefs; 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 @@ public class Jar extends Zip { /** 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 @@ public class Jar extends Zip { 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 @@ public class Jar extends Zip { // 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; } diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index a569158a7..e0f57f2e9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -174,6 +174,14 @@ public class Zip extends MatchingTask { this.zipFile = destFile; } + /** + * The file to create. + * @since Ant 1.5.2 + */ + public File getDestFile() { + return zipFile; + } + /** * Directory from which to archive files; optional. @@ -190,6 +198,15 @@ public class Zip extends MatchingTask { 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; * optional, defaults to false. @@ -283,6 +300,15 @@ public class Zip extends MatchingTask { this.encoding = encoding; } + /** + * Encoding to use for filenames. + * + * @since Ant 1.5.2 + */ + public String getEncoding() { + return encoding; + } + /** * validate and build */ @@ -597,7 +623,7 @@ public class Zip extends MatchingTask { * * @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.