From ee0fc90c1af3f49fbfc357258ccf63aafd230b10 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 3 Nov 2008 16:40:42 +0000 Subject: [PATCH] make sure log messages to get emmitted twice in double-pass mode. PR 39426. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@710101 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Ear.java | 2 +- .../org/apache/tools/ant/taskdefs/Jar.java | 16 +++-- .../org/apache/tools/ant/taskdefs/War.java | 2 +- .../org/apache/tools/ant/taskdefs/Zip.java | 72 +++++++++++++++---- 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Ear.java b/src/main/org/apache/tools/ant/taskdefs/Ear.java index f7f9ca639..9cf96aa1d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ear.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ear.java @@ -130,7 +130,7 @@ public class Ear extends Jar { if (deploymentDescriptor != null || !FILE_UTILS.fileNameEquals(deploymentDescriptor, file) || descriptorAdded) { - log("Warning: selected " + archiveType + logOnFirstPass("Warning: selected " + archiveType + " files include a " + XML_DESCRIPTOR_PATH + " which will" + " be ignored (please use appxml attribute to " + archiveType + " task)", diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index d63b24844..a420dd74f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -632,11 +632,11 @@ public class Jar extends Zip { long lastModified, File fromArchive, int mode) throws IOException { if (MANIFEST_NAME.equalsIgnoreCase(vPath)) { - if (!doubleFilePass || skipWriting) { + if (isFirstPass()) { filesetManifest(fromArchive, is); } } else if (INDEX_NAME.equalsIgnoreCase(vPath) && index) { - log("Warning: selected " + archiveType + logOnFirstPass("Warning: selected " + archiveType + " files include a " + INDEX_NAME + " which will" + " be replaced by a newly generated one.", Project.MSG_WARN); } else { @@ -671,7 +671,7 @@ public class Jar extends Zip { } else if (filesetManifestConfig != null && !filesetManifestConfig.getValue().equals("skip")) { // we add this to our group of fileset manifests - log("Found manifest to merge in file " + file, + logOnFirstPass("Found manifest to merge in file " + file, Project.MSG_VERBOSE); try { @@ -754,13 +754,13 @@ public class Jar extends Zip { try { originalManifest = getManifestFromJar(zipFile); if (originalManifest == null) { - log("Updating jar since the current jar has no manifest", + logOnFirstPass("Updating jar since the current jar has no manifest", Project.MSG_VERBOSE); needsUpdate = true; } else { Manifest mf = createManifest(); if (!mf.equals(originalManifest)) { - log("Updating jar since jar manifest has changed", + logOnFirstPass("Updating jar since jar manifest has changed", Project.MSG_VERBOSE); needsUpdate = true; } @@ -801,9 +801,11 @@ public class Jar extends Zip { } if (emptyBehavior.equals("skip")) { + if (!skipWriting) { log("Warning: skipping " + archiveType + " archive " + zipFile + " because no files were included.", Project.MSG_WARN); + } return true; } else if (emptyBehavior.equals("fail")) { throw new BuildException("Cannot create " + archiveType @@ -814,8 +816,10 @@ public class Jar extends Zip { ZipOutputStream zOut = null; try { + if (!skipWriting) { log("Building MANIFEST-only jar: " + getDestFile().getAbsolutePath()); + } zOut = new ZipOutputStream(new FileOutputStream(getDestFile())); zOut.setEncoding(getEncoding()); @@ -894,7 +898,7 @@ public class Jar extends Zip { if (strict.getValue().equalsIgnoreCase("fail")) { throw new BuildException(message.toString(), getLocation()); } else { - log(message.toString(), strict.getLogLevel()); + logOnFirstPass(message.toString(), strict.getLogLevel()); } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/War.java b/src/main/org/apache/tools/ant/taskdefs/War.java index 9940a443f..7d9937b5a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/War.java +++ b/src/main/org/apache/tools/ant/taskdefs/War.java @@ -188,7 +188,7 @@ public class War extends Jar { addFile = false; //check to see if we warn or not if (!FILE_UTILS.fileNameEquals(addedWebXmlFile, file)) { - log("Warning: selected " + archiveType + logOnFirstPass("Warning: selected " + archiveType + " files include a second " + XML_DESCRIPTOR_PATH + " which will be ignored.\n" + "The duplicate entry is at " + file + '\n' diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index eb841b564..6259fa198 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -93,9 +93,37 @@ public class Zip extends MatchingTask { protected Hashtable addedDirs = new Hashtable(); private Vector addedFiles = new Vector(); + /** + * If this flag is true, execute() will run most operations twice, + * the first time with {@link #skipWriting skipWriting} set to + * true and the second time with setting it to false. + * + *

The only situation in Ant's current code base where this is + * ever going to be true is if the jar task has been configured + * with a filesetmanifest other than "skip".

+ */ protected boolean doubleFilePass = false; + /** + * whether the methods should just perform some sort of dry-run. + * + *

Will only ever be true in the first pass if the task + * performs two passes because {@link #doubleFilePass + * doubleFilePass} is true.

+ */ protected boolean skipWriting = false; + /** + * Whether this is the first time the archive building methods are invoked. + * + * @return true if either {@link #doubleFilePass doubleFilePass} + * is false or {@link #skipWriting skipWriting} is true. + * + * @since Ant 1.8.0 + */ + protected final boolean isFirstPass() { + return !doubleFilePass || skipWriting; + } + private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); // CheckStyle:VisibilityModifier ON @@ -502,7 +530,9 @@ public class Zip extends MatchingTask { String action = doUpdate ? "Updating " : "Building "; - log(action + archiveType + ": " + zipFile.getAbsolutePath()); + if (!skipWriting) { + log(action + archiveType + ": " + zipFile.getAbsolutePath()); + } ZipOutputStream zOut = null; try { @@ -673,7 +703,7 @@ public class Zip extends MatchingTask { // we don't need to update if the original file doesn't exist if (doUpdate && !zipFile.exists()) { doUpdate = false; - log("ignoring update attribute as " + archiveType + logOnFirstPass("ignoring update attribute as " + archiveType + " doesn't exist.", Project.MSG_DEBUG); } } @@ -683,14 +713,14 @@ public class Zip extends MatchingTask { // Add the files found in groupfileset to fileset for (int i = 0; i < groupfilesets.size(); i++) { - log("Processing groupfileset ", Project.MSG_VERBOSE); + logOnFirstPass("Processing groupfileset ", Project.MSG_VERBOSE); FileSet fs = (FileSet) groupfilesets.elementAt(i); FileScanner scanner = fs.getDirectoryScanner(getProject()); String[] files = scanner.getIncludedFiles(); File basedir = scanner.getBasedir(); for (int j = 0; j < files.length; j++) { - log("Adding file " + files[j] + " to fileset", + logOnFirstPass("Adding file " + files[j] + " to fileset", Project.MSG_VERBOSE); ZipFileSet zf = new ZipFileSet(); zf.setProject(getProject()); @@ -958,8 +988,10 @@ public class Zip extends MatchingTask { // In this case using java.util.zip will not work // because it does not permit a zero-entry archive. // Must create it manually. - log("Note: creating empty " + archiveType + " archive " + zipFile, - Project.MSG_INFO); + if (!skipWriting) { + log("Note: creating empty " + archiveType + " archive " + zipFile, + Project.MSG_INFO); + } OutputStream os = null; try { os = new FileOutputStream(zipFile); @@ -1120,11 +1152,11 @@ public class Zip extends MatchingTask { if (emptyBehavior.equals("skip")) { if (doUpdate) { - log(archiveType + " archive " + zipFile + logOnFirstPass(archiveType + " archive " + zipFile + " not updated because no new files were included.", Project.MSG_VERBOSE); } else { - log("Warning: skipping " + archiveType + " archive " + logOnFirstPass("Warning: skipping " + archiveType + " archive " + zipFile + " because no files were included.", Project.MSG_WARN); } @@ -1425,7 +1457,7 @@ public class Zip extends MatchingTask { int mode, ZipExtraField[] extra) throws IOException { if (doFilesonly) { - log("skipping directory " + vPath + " for file-only archive", + logOnFirstPass("skipping directory " + vPath + " for file-only archive", Project.MSG_VERBOSE); return; } @@ -1435,7 +1467,7 @@ public class Zip extends MatchingTask { return; } - log("adding directory " + vPath, Project.MSG_VERBOSE); + logOnFirstPass("adding directory " + vPath, Project.MSG_VERBOSE); addedDirs.put(vPath, vPath); if (!skipWriting) { @@ -1483,7 +1515,7 @@ public class Zip extends MatchingTask { if (entries.contains(vPath)) { if (duplicate.equals("preserve")) { - log(vPath + " already added, skipping", Project.MSG_INFO); + logOnFirstPass(vPath + " already added, skipping", Project.MSG_INFO); return; } else if (duplicate.equals("fail")) { throw new BuildException("Duplicate file " + vPath @@ -1491,11 +1523,11 @@ public class Zip extends MatchingTask { + "attribute is 'fail'."); } else { // duplicate equal to add, so we continue - log("duplicate file " + vPath + logOnFirstPass("duplicate file " + vPath + " found, adding.", Project.MSG_VERBOSE); } } else { - log("adding entry " + vPath, Project.MSG_VERBOSE); + logOnFirstPass("adding entry " + vPath, Project.MSG_VERBOSE); } entries.put(vPath, vPath); @@ -1712,7 +1744,7 @@ public class Zip extends MatchingTask { if (!orig[i].isDirectory()) { v.addElement(orig[i]); } else { - log("Ignoring directory " + orig[i].getName() + logOnFirstPass("Ignoring directory " + orig[i].getName() + " as only files will be added.", Project.MSG_VERBOSE); } } @@ -1725,6 +1757,18 @@ public class Zip extends MatchingTask { return orig; } + /** + * Logs a message at the given output level, but only if this is + * the {@link #isFirstPass first pass}. + * + * @since Ant 1.8.0 + */ + protected void logOnFirstPass(String msg, int level) { + if (isFirstPass()) { + log(msg, level); + } + } + /** * Possible behaviors when a duplicate file is added: * "add", "preserve" or "fail"