Browse Source

fix the 'is the manifest up-to-date?' checks

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273991 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
34dc5127ac
2 changed files with 54 additions and 64 deletions
  1. +50
    -64
      src/main/org/apache/tools/ant/taskdefs/Jar.java
  2. +4
    -0
      src/testcases/org/apache/tools/ant/taskdefs/JarTest.java

+ 50
- 64
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -92,6 +92,9 @@ public class Jar extends Zip {
/** The index file name. */ /** The index file name. */
private static final String INDEX_NAME = "META-INF/INDEX.LIST"; private static final String INDEX_NAME = "META-INF/INDEX.LIST";


/** The mainfest file name. */
private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";

/** merged manifests added through addConfiguredManifest */ /** merged manifests added through addConfiguredManifest */
private Manifest configuredManifest; private Manifest configuredManifest;
/** shadow of the above if upToDate check alters the value */ /** shadow of the above if upToDate check alters the value */
@@ -163,35 +166,6 @@ public class Jar extends Zip {
*/ */
public void setDestFile(File jarFile) { public void setDestFile(File jarFile) {
super.setDestFile(jarFile); super.setDestFile(jarFile);
if (jarFile.exists()) {
ZipFile zf = null;
try {
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);
} finally {
if (zf != null) {
try {
zf.close();
} catch (IOException e) {
// XXX - log an error? throw an exception?
}
}
}
}
} }


/** /**
@@ -258,6 +232,33 @@ public class Jar extends Zip {
return newManifest; return newManifest;
} }


private Manifest getManifestFromJar(File jarFile) throws IOException {
ZipFile zf = null;
try {
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(MANIFEST_NAME)) {
return getManifest(new InputStreamReader(zf
.getInputStream(ze)));
}
}
return null;
} finally {
if (zf != null) {
try {
zf.close();
} catch (IOException e) {
// XXX - log an error? throw an exception?
}
}
}
}

private Manifest getManifest(Reader r) { private Manifest getManifest(Reader r) {


Manifest newManifest = null; Manifest newManifest = null;
@@ -321,10 +322,6 @@ public class Jar extends Zip {
private Manifest createManifest() private Manifest createManifest()
throws BuildException { throws BuildException {
try { try {
if (!isInUpdateMode()) {
originalManifest = null;
}

Manifest finalManifest = Manifest.getDefaultManifest(); Manifest finalManifest = Manifest.getDefaultManifest();


if (manifest == null) { if (manifest == null) {
@@ -343,7 +340,9 @@ public class Jar extends Zip {
* merge with null argument is a no-op * merge with null argument is a no-op
*/ */


finalManifest.merge(originalManifest);
if (isInUpdateMode()) {
finalManifest.merge(originalManifest);
}
finalManifest.merge(filesetManifest); finalManifest.merge(filesetManifest);
finalManifest.merge(configuredManifest); finalManifest.merge(configuredManifest);
finalManifest.merge(manifest, !mergeManifestsMain); finalManifest.merge(manifest, !mergeManifestsMain);
@@ -373,7 +372,7 @@ public class Jar extends Zip {


ByteArrayInputStream bais = ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray()); new ByteArrayInputStream(baos.toByteArray());
super.zipFile(bais, zOut, "META-INF/MANIFEST.MF",
super.zipFile(bais, zOut, MANIFEST_NAME,
System.currentTimeMillis(), null, System.currentTimeMillis(), null,
ZipFileSet.DEFAULT_FILE_MODE); ZipFileSet.DEFAULT_FILE_MODE);
super.initZipOutputStream(zOut); super.initZipOutputStream(zOut);
@@ -448,7 +447,7 @@ public class Jar extends Zip {
protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath, protected void zipFile(InputStream is, ZipOutputStream zOut, String vPath,
long lastModified, File fromArchive, int mode) long lastModified, File fromArchive, int mode)
throws IOException { throws IOException {
if ("META-INF/MANIFEST.MF".equalsIgnoreCase(vPath)) {
if (MANIFEST_NAME.equalsIgnoreCase(vPath)) {
if (! doubleFilePass || (doubleFilePass && skipWriting)) { if (! doubleFilePass || (doubleFilePass && skipWriting)) {
filesetManifest(fromArchive, is); filesetManifest(fromArchive, is);
} }
@@ -536,45 +535,32 @@ public class Jar extends Zip {
throws BuildException { throws BuildException {


// need to handle manifest as a special check // need to handle manifest as a special check
if (configuredManifest != null || manifestFile == null) {
java.util.zip.ZipFile theZipFile = null;
if (zipFile.exists()) {
// if it doesn't exist, it will get created anyway, don't
// bother with any up-to-date checks.

try { try {
theZipFile = new java.util.zip.ZipFile(zipFile);
java.util.zip.ZipEntry entry =
theZipFile.getEntry("META-INF/MANIFEST.MF");
if (entry == null) {
originalManifest = getManifestFromJar(zipFile);
if (originalManifest == null) {
log("Updating jar since the current jar has no manifest", log("Updating jar since the current jar has no manifest",
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
needsUpdate = true; needsUpdate = true;
} else { } else {
Manifest currentManifest =
new Manifest(new InputStreamReader(theZipFile
.getInputStream(entry)));
Manifest newManifest = createManifest();
if (!currentManifest.equals(newManifest)) {
log("Updating jar since jar manifest has changed",
Manifest mf = createManifest();
if (!mf.equals(originalManifest)) {
log("Updating jar since jar manifest has changed",
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
needsUpdate = true; needsUpdate = true;
} }
} }
} catch (Exception e) {
// any problems and we will rebuild
log("Updating jar since cannot read current jar manifest: "
+ e.getClass().getName() + " - " + e.getMessage(),
Project.MSG_VERBOSE);
} catch (Throwable t) {
log("error while reading original manifest: " + t.getMessage(),
Project.MSG_WARN);
needsUpdate = true; needsUpdate = true;
} finally {
if (theZipFile != null) {
try {
theZipFile.close();
} catch (IOException e) {
//ignore
}
}
} }
} else if (manifestFile.lastModified() > zipFile.lastModified()) {
log("Updating jar since manifestFile is newer than the archive",
Project.MSG_VERBOSE);

} else {
// no existing archive
needsUpdate = true; needsUpdate = true;
} }


+ 4
- 0
src/testcases/org/apache/tools/ant/taskdefs/JarTest.java View File

@@ -79,6 +79,10 @@ public class JarTest extends BuildFileTest {
executeTarget("cleanup"); executeTarget("cleanup");
} }
// public static junit.framework.Test suite() {
// return new JarTest("testNoRecreateWithoutUpdate");
// }

public void test1() { public void test1() {
expectBuildException("test1", "required argument not specified"); expectBuildException("test1", "required argument not specified");
} }


Loading…
Cancel
Save