bodewig 2003/03/12 03:29:10
Modified: src/etc/testcases/taskdefs Tag: ANT_15_BRANCH jar.xml src/main/org/apache/tools/ant/taskdefs Tag: ANT_15_BRANCH Jar.java Zip.java src/testcases/org/apache/tools/ant/taskdefs Tag: ANT_15_BRANCH JarTest.java Log: Merge tests and fix for bug 17780 from HEAD Revision Changes Path No revision No revision 1.4.2.4 +11 -0 ant/src/etc/testcases/taskdefs/jar.xml Index: jar.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/jar.xml,v retrieving revision 1.4.2.3 retrieving revision 1.4.2.4 diff -u -r1.4.2.3 -r1.4.2.4 --- jar.xml 26 Feb 2003 10:07:00 -0000 1.4.2.3 +++ jar.xml 12 Mar 2003 11:29:09 -0000 1.4.2.4 @@ -189,4 +189,15 @@ </jar> </target> + <!-- bug 17780 --> + <target name="testUpdateIfOnlyManifestHasChanged" + depends="test4"> + <jar destfile="${tmp.jar}" update="true"> + <manifest> + <attribute name="Foo" value="bar"/> + </manifest> + </jar> + <mkdir dir="${tmp.dir}"/> + <unzip src="${tmp.jar}" dest="${tmp.dir}"/> + </target> </project> No revision No revision 1.51.2.17 +5 -3 ant/src/main/org/apache/tools/ant/taskdefs/Jar.java Index: Jar.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Jar.java,v retrieving revision 1.51.2.16 retrieving revision 1.51.2.17 diff -u -r1.51.2.16 -r1.51.2.17 --- Jar.java 19 Feb 2003 08:13:59 -0000 1.51.2.16 +++ Jar.java 12 Mar 2003 11:29:10 -0000 1.51.2.17 @@ -535,11 +535,12 @@ * out-of-date. Subclasses overriding this method are supposed to * set this value correctly in their call to * super.getResourcesToAdd. - * @return an array of resources to add for each fileset passed in. + * @return an array of resources to add for each fileset passed in as well + * as a flag that indicates whether the archive is uptodate. * * @exception BuildException if it likes */ - protected Resource[][] getResourcesToAdd(FileSet[] filesets, + protected ArchiveState getResourcesToAdd(FileSet[] filesets, File zipFile, boolean needsUpdate) throws BuildException { @@ -585,7 +586,8 @@ ZipOutputStream zOut = null; try { - log("Building jar: " + getDestFile().getAbsolutePath()); + log("Building MANIFEST-only jar: " + + getDestFile().getAbsolutePath()); zOut = new ZipOutputStream(new FileOutputStream(getDestFile())); zOut.setEncoding(getEncoding()); 1.78.2.13 +64 -11 ant/src/main/org/apache/tools/ant/taskdefs/Zip.java Index: Zip.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v retrieving revision 1.78.2.12 retrieving revision 1.78.2.13 diff -u -r1.78.2.12 -r1.78.2.13 --- Zip.java 7 Mar 2003 12:38:39 -0000 1.78.2.12 +++ Zip.java 12 Mar 2003 11:29:10 -0000 1.78.2.13 @@ -327,7 +327,11 @@ // we don't need to update if the original file doesn't exist addingNewFiles = true; - doUpdate = doUpdate && zipFile.exists(); + if (doUpdate && !zipFile.exists()) { + doUpdate = false; + log("ignoring update attribute as " + archiveType + + " doesn't exist.", Project.MSG_DEBUG); + } // Add the files found in groupfileset to fileset for (int i = 0; i < groupfilesets.size(); i++) { @@ -364,14 +368,16 @@ vfss.copyInto(fss); boolean success = false; try { - Resource[][] addThem = getResourcesToAdd(fss, zipFile, false); + // can also handle empty archives + ArchiveState state = getResourcesToAdd(fss, zipFile, false); // quick exit if the target is up to date - // can also handle empty archives - if (isEmpty(addThem)) { + if (!state.isOutOfDate()) { return; } + Resource[][] addThem = state.getResourcesToAdd(); + if (doUpdate) { renamedFile = fileUtils.createTempFile("zip", ".tmp", @@ -666,17 +672,38 @@ * out-of-date. Subclasses overriding this method are supposed to * set this value correctly in their call to * super.getResourcesToAdd. - * @return an array of resources to add for each fileset passed in. + * @return an array of resources to add for each fileset passed in as well + * as a flag that indicates whether the archive is uptodate. * * @exception BuildException if it likes */ - protected Resource[][] getResourcesToAdd(FileSet[] filesets, + protected ArchiveState getResourcesToAdd(FileSet[] filesets, File zipFile, boolean needsUpdate) throws BuildException { Resource[][] initialResources = grabResources(filesets); if (isEmpty(initialResources)) { + if (needsUpdate && doUpdate) { + /* + * This is a rather hairy case. + * + * One of our subclasses knows that we need to update the + * archive, but at the same time, there are no resources + * known to us that would need to be added. Only the + * subclass seems to know what's going on. + * + * This happens if <jar> detects that the manifest has changed, + * for example. The manifest is not part of any resources + * because of our support for inline <manifest>s. + * + * If we invoke createEmptyZip like Ant 1.5.2 did, + * we'll loose all stuff that has been in the original + * archive (bugzilla report 17780). + */ + return new ArchiveState(true, initialResources); + } + if (emptyBehavior.equals("skip")) { if (doUpdate) { log(archiveType + " archive " + zipFile @@ -696,16 +723,18 @@ // Create. createEmptyZip(zipFile); } - return initialResources; + return new ArchiveState(needsUpdate, initialResources); } + // initialResources is not empty + if (!zipFile.exists()) { - return initialResources; + return new ArchiveState(true, initialResources); } if (needsUpdate && !doUpdate) { // we are recreating the archive, need all resources - return initialResources; + return new ArchiveState(true, initialResources); } Resource[][] newerResources = new Resource[filesets.length][]; @@ -773,10 +802,10 @@ if (needsUpdate && !doUpdate) { // we are recreating the archive, need all resources - return initialResources; + return new ArchiveState(true, initialResources); } - return newerResources; + return new ArchiveState(needsUpdate, newerResources); } /** @@ -1068,6 +1097,30 @@ public static class Duplicate extends EnumeratedAttribute { public String[] getValues() { return new String[] {"add", "preserve", "fail"}; + } + } + + /** + * Holds the up-to-date status and the out-of-date resources of + * the original archive. + * + * @since Ant 1.5.3 + */ + public static class ArchiveState { + private boolean outOfDate; + private Resource[][] resourcesToAdd; + + ArchiveState(boolean state, Resource[][] r) { + outOfDate = state; + resourcesToAdd = r; + } + + public boolean isOutOfDate() { + return outOfDate; + } + + public Resource[][] getResourcesToAdd() { + return resourcesToAdd; } } } No revision No revision 1.8.2.6 +9 -2 ant/src/testcases/org/apache/tools/ant/taskdefs/JarTest.java Index: JarTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/JarTest.java,v retrieving revision 1.8.2.5 retrieving revision 1.8.2.6 diff -u -r1.8.2.5 -r1.8.2.6 --- JarTest.java 26 Feb 2003 10:07:00 -0000 1.8.2.5 +++ JarTest.java 12 Mar 2003 11:29:10 -0000 1.8.2.6 @@ -67,6 +67,7 @@ public class JarTest extends BuildFileTest { private static String tempJar = "tmp.jar"; + private static String tempDir = "jartmp/"; private Reader r1, r2; public JarTest(String name) { @@ -170,9 +171,9 @@ executeTarget("testManifestStaysIntact"); r1 = new FileReader(getProject() - .resolveFile("jartmp/manifest")); + .resolveFile(tempDir + "manifest")); r2 = new FileReader(getProject() - .resolveFile("jartmp/META-INF/MANIFEST.MF")); + .resolveFile(tempDir + "META-INF/MANIFEST.MF")); Manifest mf1 = new Manifest(r1); Manifest mf2 = new Manifest(r2); assertEquals(mf1, mf2); @@ -218,5 +219,11 @@ executeTarget("testCreateWithEmptyFilesetSetUp"); executeTarget("testCreateWithEmptyFileset"); executeTarget("testCreateWithEmptyFileset"); + } + + public void testUpdateIfOnlyManifestHasChanged() { + executeTarget("testUpdateIfOnlyManifestHasChanged"); + File jarXml = getProject().resolveFile(tempDir + "jar.xml"); + assertTrue(jarXml.exists()); } }