bodewig 2003/01/17 04:30:44 Modified: . WHATSNEW src/main/org/apache/tools/ant/taskdefs Jar.java src/testcases/org/apache/tools/ant/taskdefs JarTest.java Log: Take care of the original manifest when updating a jar. PR: 12651 Based on code submitted by: Ben Jenkins <bjenkins at dynamicsoft dot com> Revision Changes Path 1.343 +2 -0 jakarta-ant/WHATSNEW Index: WHATSNEW =================================================================== RCS file: /home/cvs/jakarta-ant/WHATSNEW,v retrieving revision 1.342 retrieving revision 1.343 diff -u -r1.342 -r1.343 --- WHATSNEW 16 Jan 2003 12:08:25 -0000 1.342 +++ WHATSNEW 17 Jan 2003 12:30:43 -0000 1.343 @@ -94,6 +94,8 @@ * build.sysclasspath will now be honored by more tasks. +* <jar update="true"> would remove the original manifest. + Other changes: -------------- * The filesetmanifest attribute of <jar> has been reenabled. 1.59 +69 -30 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.58 retrieving revision 1.59 diff -u -r1.58 -r1.59 --- Jar.java 9 Dec 2002 12:40:38 -0000 1.58 +++ Jar.java 17 Jan 2003 12:30:44 -0000 1.59 @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -65,6 +65,8 @@ import java.io.PrintWriter; import java.io.Reader; import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.FileScanner; import org.apache.tools.ant.Project; @@ -96,6 +98,12 @@ /** merged manifests added through filesets */ private Manifest filesetManifest; + /** + * Manifest of original archive, will be set to null if not in + * update mode. + */ + private Manifest originalManifest; + /** * whether to merge fileset manifests; * value is true if filesetmanifest is 'merge' or 'mergewithoutmain' @@ -146,6 +154,36 @@ } /** + * Override to get hold of the original Manifest (if present and + * only if updating ... + * + * @since Ant 1.5.2 + */ + public void setDestFile(File jarFile) { + super.setDestFile(jarFile); + if (jarFile.exists()) { + try { + ZipFile zf = new ZipFile(jarFile); + + // must not use getEntry as "well behaving" applications + // must accept the manifest in any capitalization + Enumeration enum = zf.entries(); + while (enum.hasMoreElements()) { + ZipEntry ze = (ZipEntry) enum.nextElement(); + if (ze.getName().equalsIgnoreCase("META-INF/MANIFEST.MF")) { + originalManifest = + getManifest(new InputStreamReader(zf + .getInputStream(ze))); + } + } + } catch (Throwable t) { + log("error while reading original manifest: " + t.getMessage(), + Project.MSG_WARN); + } + } + } + + /** * Set whether or not to create an index list for classes. * This may speed up classloading in some cases. */ @@ -272,6 +310,10 @@ private Manifest createManifest() throws BuildException { try { + if (!isInUpdateMode()) { + originalManifest = null; + } + Manifest finalManifest = Manifest.getDefaultManifest(); if (manifest == null) { @@ -279,25 +321,22 @@ // if we haven't got the manifest yet, attempt to // get it now and have manifest be the final merge manifest = getManifest(manifestFile); - finalManifest.merge(filesetManifest); - finalManifest.merge(configuredManifest); - finalManifest.merge(manifest, !mergeManifestsMain); - } else if (configuredManifest != null) { - // configuredManifest is the final merge - finalManifest.merge(filesetManifest); - finalManifest.merge(configuredManifest, - !mergeManifestsMain); - } else if (filesetManifest != null) { - // filesetManifest is the final (and only) merge - finalManifest.merge(filesetManifest, !mergeManifestsMain); } - } else { - // manifest is the final merge - finalManifest.merge(filesetManifest); - finalManifest.merge(configuredManifest); - finalManifest.merge(manifest, !mergeManifestsMain); } + /* + * Precedence: manifestFile wins over inline manifest, + * over manifests read from the filesets over the original + * manifest. + * + * merge with null argument is a no-op + */ + + finalManifest.merge(originalManifest); + finalManifest.merge(filesetManifest); + finalManifest.merge(configuredManifest); + finalManifest.merge(manifest, !mergeManifestsMain); + return finalManifest; } catch (ManifestException e) { @@ -307,7 +346,7 @@ } private void writeManifest(ZipOutputStream zOut, Manifest manifest) - throws IOException { + throws IOException { for (Enumeration e = manifest.getWarnings(); e.hasMoreElements();) { log("Manifest warning: " + (String) e.nextElement(), @@ -330,7 +369,7 @@ } protected void finalizeZipOutputStream(ZipOutputStream zOut) - throws IOException, BuildException { + throws IOException, BuildException { if (index) { createIndexList(zOut); @@ -399,9 +438,9 @@ int mode) throws IOException { if ("META-INF/MANIFEST.MF".equalsIgnoreCase(vPath)) { - if (! doubleFilePass || (doubleFilePass && skipWriting)) { - filesetManifest(file, null); - } + if (! doubleFilePass || (doubleFilePass && skipWriting)) { + filesetManifest(file, null); + } } else { super.zipFile(file, zOut, vPath, mode); } @@ -414,9 +453,9 @@ long lastModified, File file, int mode) throws IOException { if ("META-INF/MANIFEST.MF".equalsIgnoreCase(vPath)) { - if (! doubleFilePass || (doubleFilePass && skipWriting)) { - filesetManifest(file, is); - } + if (! doubleFilePass || (doubleFilePass && skipWriting)) { + filesetManifest(file, is); + } } else { super.zipFile(is, zOut, vPath, lastModified, null, mode); } @@ -542,11 +581,11 @@ // we want to save this info if we are going to make another pass if (! doubleFilePass || (doubleFilePass && ! skipWriting)) - { - manifest = null; - configuredManifest = savedConfiguredManifest; - filesetManifest = null; - } + { + manifest = null; + configuredManifest = savedConfiguredManifest; + filesetManifest = null; + } } /** 1.11 +1 -1 jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/JarTest.java Index: JarTest.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/JarTest.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- JarTest.java 17 Jan 2003 10:50:57 -0000 1.10 +++ JarTest.java 17 Jan 2003 12:30:44 -0000 1.11 @@ -152,7 +152,7 @@ jarModifiedDate < jarFile.lastModified()); } - public void XtestManifestStaysIntact() + public void testManifestStaysIntact() throws IOException, ManifestException { executeTarget("testManifestStaysIntact"); Manifest mf1 =
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>