diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java index 8d174b8ba..4986b49b3 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java @@ -56,12 +56,14 @@ package org.apache.tools.ant.taskdefs; import java.io.*; import org.apache.tools.ant.*; +import org.apache.tools.ant.util.*; import org.apache.tools.tar.*; /** * Creates a TAR archive. * * @author Stefano Mazzocchi stefano@apache.org + * @author Stefan Bodewig */ public class Tar extends MatchingTask { @@ -72,15 +74,15 @@ public class Tar extends MatchingTask { /** * This is the name/location of where to create the tar file. */ - public void setTarfile(String tarFilename) { - tarFile = project.resolveFile(tarFilename); + public void setTarfile(File tarFile) { + this.tarFile = tarFile; } /** * This is the base directory to look in for things to tar. */ - public void setBasedir(String baseDirname) { - baseDir = project.resolveFile(baseDirname); + public void setBasedir(File baseDir) { + this.baseDir = baseDir; } public void execute() throws BuildException { @@ -97,12 +99,18 @@ public class Tar extends MatchingTask { throw new BuildException("basedir does not exist!", location); } - log("Building tar: "+ tarFile.getAbsolutePath()); - DirectoryScanner ds = super.getDirectoryScanner(baseDir); String[] files = ds.getIncludedFiles(); + if (archiveIsUpToDate(files)) { + log("Nothing to do: "+tarFile.getAbsolutePath()+" is up to date.", + Project.MSG_INFO); + return; + } + + log("Building tar: "+ tarFile.getAbsolutePath(), Project.MSG_INFO); + TarOutputStream tOut = null; try { tOut = new TarOutputStream(new FileOutputStream(tarFile)); @@ -150,4 +158,11 @@ public class Tar extends MatchingTask { fIn.close(); } } + + protected boolean archiveIsUpToDate(String[] files) { + SourceFileScanner sfs = new SourceFileScanner(this); + MergingMapper mm = new MergingMapper(); + mm.setTo(tarFile.getAbsolutePath()); + return sfs.restrict(files, baseDir, null, mm).length == 0; + } } diff --git a/src/main/org/apache/tools/ant/taskdefs/UpToDate.java b/src/main/org/apache/tools/ant/taskdefs/UpToDate.java index 9f515ba26..277894463 100644 --- a/src/main/org/apache/tools/ant/taskdefs/UpToDate.java +++ b/src/main/org/apache/tools/ant/taskdefs/UpToDate.java @@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; import org.apache.tools.ant.types.*; +import org.apache.tools.ant.util.*; import java.io.*; import java.util.Enumeration; import java.util.Date; @@ -67,7 +68,7 @@ import java.util.Vector; * * @author William Ferguson williamf@mincom.com * @author Hiroaki Nakamura hnakamur@mc.neweb.ne.jp - * @author Stefan Bodewig + * @author Stefan Bodewig */ public class UpToDate extends MatchingTask { @@ -137,26 +138,9 @@ public class UpToDate extends MatchingTask { } protected boolean scanDir(File srcDir, File destFile, String files[]) { - long destLastModified = destFile.lastModified(); - long now = (new Date()).getTime(); - if (destLastModified > now) { - log("Warning: destfile modified in the future: " + - destFile.getPath(), Project.MSG_WARN); - } - - for (int i = 0; i < files.length; i++) { - File srcFile = new File(srcDir, files[i]); - - long srcLastModified = srcFile.lastModified(); - if (srcLastModified > now) { - log("Warning: file modified in the future: " + - files[i], Project.MSG_WARN); - } - - if (srcLastModified > destLastModified) { - return false; - } - } - return true; + SourceFileScanner sfs = new SourceFileScanner(this); + MergingMapper mm = new MergingMapper(); + mm.setTo(destFile.getAbsolutePath()); + return sfs.restrict(files, srcDir, null, mm).length == 0; } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index 2cd5274a3..77768650e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; import org.apache.tools.ant.types.*; +import org.apache.tools.ant.util.*; import java.io.*; import java.util.Enumeration; @@ -70,6 +71,7 @@ import java.util.zip.*; * * @author James Davidson duncan@x180.com * @author Jon S. Stevens jon@clearink.com + * @author Stefan Bodewig */ public class Zip extends MatchingTask { @@ -96,15 +98,15 @@ public class Zip extends MatchingTask { * This is the base directory to look in for * things to zip. */ - public void setBasedir(String baseDirname) { - baseDir = project.resolveFile(baseDirname); + public void setBasedir(File baseDir) { + this.baseDir = baseDir; } /** * Sets whether we want to compress the files or only store them. */ - public void setCompress(String compress) { - doCompress = Project.toBoolean(compress); + public void setCompress(boolean c) { + doCompress = c; } /** @@ -228,7 +230,8 @@ public class Zip extends MatchingTask { */ protected boolean isUpToDate(FileScanner[] scanners, File zipFile) throws BuildException { - File[] files = grabFiles(scanners); + String[][] fileNames = grabFileNames(scanners); + File[] files = grabFiles(scanners, fileNames); if (files.length == 0) { if (emptyBehavior.equals("skip")) { log("Warning: skipping "+archiveType+" archive " + zipFile + @@ -264,10 +267,14 @@ public class Zip extends MatchingTask { return true; } } else { - // Probably unnecessary but just for clarity: if (!zipFile.exists()) return false; - for (int i=0; i zipFile.lastModified()) { + + SourceFileScanner sfs = new SourceFileScanner(this); + MergingMapper mm = new MergingMapper(); + mm.setTo(zipFile.getAbsolutePath()); + for (int i=0; i 0) { return false; } } @@ -276,18 +283,30 @@ public class Zip extends MatchingTask { } protected static File[] grabFiles(FileScanner[] scanners) { - Vector files = new Vector (); - for (int i = 0; i < scanners.length; i++) { + return grabFiles(scanners, grabFileNames(scanners)); + } + + protected static File[] grabFiles(FileScanner[] scanners, + String[][] fileNames) { + Vector files = new Vector(); + for (int i = 0; i < fileNames.length; i++) { File thisBaseDir = scanners[i].getBasedir(); - String[] ifiles = scanners[i].getIncludedFiles(); - for (int j = 0; j < ifiles.length; j++) - files.addElement(new File(thisBaseDir, ifiles[j])); + for (int j = 0; j < fileNames[i].length; j++) + files.addElement(new File(thisBaseDir, fileNames[i][j])); } File[] toret = new File[files.size()]; files.copyInto(toret); return toret; } + protected static String[][] grabFileNames(FileScanner[] scanners) { + String[][] result = new String[scanners.length][]; + for (int i=0; i. + */ + +package org.apache.tools.ant.util; + +/** + * Implementation of FileNameMapper that always returns the same + * target file name. + * + *

This is the default FileNameMapper for the archiving tasks and + * uptodate.

+ * + * @author Stefan Bodewig + */ +public class MergingMapper implements FileNameMapper { + protected String[] mergedFile = null; + + /** + * Ignored. + */ + public void setFrom(String from) {} + + /** + * Sets the name of the merged file. + */ + public void setTo(String to) { + mergedFile = new String[] {to}; + } + + /** + * Returns an one-element array containing the file name set via setTo. + */ + public String[] mapFileName(String sourceFileName) { + return mergedFile; + } + +} diff --git a/src/main/org/apache/tools/ant/util/SourceFileScanner.java b/src/main/org/apache/tools/ant/util/SourceFileScanner.java index ac2d12fc7..8ce61ffca 100644 --- a/src/main/org/apache/tools/ant/util/SourceFileScanner.java +++ b/src/main/org/apache/tools/ant/util/SourceFileScanner.java @@ -87,7 +87,8 @@ public class SourceFileScanner { * * @param files the original set of files * @param srcDir all files are relative to this directory - * @param destDir target files live here + * @param destDir target files live here. if null file names + * returned by the mapper are assumed to be absolute. * @param mapper knows how to construct a target file names from * source file names. */ @@ -116,7 +117,13 @@ public class SourceFileScanner { boolean added = false; targetList.setLength(0); for (int j=0; !added && j