target file. Use it in tar, zip, uptodate. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268193 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -56,12 +56,14 @@ package org.apache.tools.ant.taskdefs; | |||||
| import java.io.*; | import java.io.*; | ||||
| import org.apache.tools.ant.*; | import org.apache.tools.ant.*; | ||||
| import org.apache.tools.ant.util.*; | |||||
| import org.apache.tools.tar.*; | import org.apache.tools.tar.*; | ||||
| /** | /** | ||||
| * Creates a TAR archive. | * Creates a TAR archive. | ||||
| * | * | ||||
| * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a> | * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a> | ||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| */ | */ | ||||
| public class Tar extends MatchingTask { | 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. | * 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. | * 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 { | public void execute() throws BuildException { | ||||
| @@ -97,12 +99,18 @@ public class Tar extends MatchingTask { | |||||
| throw new BuildException("basedir does not exist!", location); | throw new BuildException("basedir does not exist!", location); | ||||
| } | } | ||||
| log("Building tar: "+ tarFile.getAbsolutePath()); | |||||
| DirectoryScanner ds = super.getDirectoryScanner(baseDir); | DirectoryScanner ds = super.getDirectoryScanner(baseDir); | ||||
| String[] files = ds.getIncludedFiles(); | 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; | TarOutputStream tOut = null; | ||||
| try { | try { | ||||
| tOut = new TarOutputStream(new FileOutputStream(tarFile)); | tOut = new TarOutputStream(new FileOutputStream(tarFile)); | ||||
| @@ -150,4 +158,11 @@ public class Tar extends MatchingTask { | |||||
| fIn.close(); | 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; | |||||
| } | |||||
| } | } | ||||
| @@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs; | |||||
| import org.apache.tools.ant.*; | import org.apache.tools.ant.*; | ||||
| import org.apache.tools.ant.types.*; | import org.apache.tools.ant.types.*; | ||||
| import org.apache.tools.ant.util.*; | |||||
| import java.io.*; | import java.io.*; | ||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||
| import java.util.Date; | import java.util.Date; | ||||
| @@ -67,7 +68,7 @@ import java.util.Vector; | |||||
| * | * | ||||
| * @author William Ferguson <a href="mailto:williamf@mincom.com">williamf@mincom.com</a> | * @author William Ferguson <a href="mailto:williamf@mincom.com">williamf@mincom.com</a> | ||||
| * @author Hiroaki Nakamura <a href="mailto:hnakamur@mc.neweb.ne.jp">hnakamur@mc.neweb.ne.jp</a> | * @author Hiroaki Nakamura <a href="mailto:hnakamur@mc.neweb.ne.jp">hnakamur@mc.neweb.ne.jp</a> | ||||
| * @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a> | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| */ | */ | ||||
| public class UpToDate extends MatchingTask { | public class UpToDate extends MatchingTask { | ||||
| @@ -137,26 +138,9 @@ public class UpToDate extends MatchingTask { | |||||
| } | } | ||||
| protected boolean scanDir(File srcDir, File destFile, String files[]) { | 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; | |||||
| } | } | ||||
| } | } | ||||
| @@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs; | |||||
| import org.apache.tools.ant.*; | import org.apache.tools.ant.*; | ||||
| import org.apache.tools.ant.types.*; | import org.apache.tools.ant.types.*; | ||||
| import org.apache.tools.ant.util.*; | |||||
| import java.io.*; | import java.io.*; | ||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||
| @@ -70,6 +71,7 @@ import java.util.zip.*; | |||||
| * | * | ||||
| * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a> | * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a> | ||||
| * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> | * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> | ||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| */ | */ | ||||
| public class Zip extends MatchingTask { | public class Zip extends MatchingTask { | ||||
| @@ -96,15 +98,15 @@ public class Zip extends MatchingTask { | |||||
| * This is the base directory to look in for | * This is the base directory to look in for | ||||
| * things to zip. | * 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. | * 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 | 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 (files.length == 0) { | ||||
| if (emptyBehavior.equals("skip")) { | if (emptyBehavior.equals("skip")) { | ||||
| log("Warning: skipping "+archiveType+" archive " + zipFile + | log("Warning: skipping "+archiveType+" archive " + zipFile + | ||||
| @@ -264,10 +267,14 @@ public class Zip extends MatchingTask { | |||||
| return true; | return true; | ||||
| } | } | ||||
| } else { | } else { | ||||
| // Probably unnecessary but just for clarity: | |||||
| if (!zipFile.exists()) return false; | if (!zipFile.exists()) return false; | ||||
| for (int i=0; i<files.length; i++) { | |||||
| if (files[i].lastModified() > zipFile.lastModified()) { | |||||
| SourceFileScanner sfs = new SourceFileScanner(this); | |||||
| MergingMapper mm = new MergingMapper(); | |||||
| mm.setTo(zipFile.getAbsolutePath()); | |||||
| for (int i=0; i<scanners.length; i++) { | |||||
| if (sfs.restrict(fileNames[i], scanners[i].getBasedir(), null, | |||||
| mm).length > 0) { | |||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| @@ -276,18 +283,30 @@ public class Zip extends MatchingTask { | |||||
| } | } | ||||
| protected static File[] grabFiles(FileScanner[] scanners) { | 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(); | 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()]; | File[] toret = new File[files.size()]; | ||||
| files.copyInto(toret); | files.copyInto(toret); | ||||
| return toret; | return toret; | ||||
| } | } | ||||
| protected static String[][] grabFileNames(FileScanner[] scanners) { | |||||
| String[][] result = new String[scanners.length][]; | |||||
| for (int i=0; i<scanners.length; i++) { | |||||
| result[i] = scanners[i].getIncludedFiles(); | |||||
| } | |||||
| return result; | |||||
| } | |||||
| protected void zipDir(File dir, ZipOutputStream zOut, String vPath) | protected void zipDir(File dir, ZipOutputStream zOut, String vPath) | ||||
| throws IOException | throws IOException | ||||
| { | { | ||||
| @@ -0,0 +1,88 @@ | |||||
| /* | |||||
| * The Apache Software License, Version 1.1 | |||||
| * | |||||
| * Copyright (c) 2000 The Apache Software Foundation. All rights | |||||
| * reserved. | |||||
| * | |||||
| * Redistribution and use in source and binary forms, with or without | |||||
| * modification, are permitted provided that the following conditions | |||||
| * are met: | |||||
| * | |||||
| * 1. Redistributions of source code must retain the above copyright | |||||
| * notice, this list of conditions and the following disclaimer. | |||||
| * | |||||
| * 2. Redistributions in binary form must reproduce the above copyright | |||||
| * notice, this list of conditions and the following disclaimer in | |||||
| * the documentation and/or other materials provided with the | |||||
| * distribution. | |||||
| * | |||||
| * 3. The end-user documentation included with the redistribution, if | |||||
| * any, must include the following acknowlegement: | |||||
| * "This product includes software developed by the | |||||
| * Apache Software Foundation (http://www.apache.org/)." | |||||
| * Alternately, this acknowlegement may appear in the software itself, | |||||
| * if and wherever such third-party acknowlegements normally appear. | |||||
| * | |||||
| * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software | |||||
| * Foundation" must not be used to endorse or promote products derived | |||||
| * from this software without prior written permission. For written | |||||
| * permission, please contact apache@apache.org. | |||||
| * | |||||
| * 5. Products derived from this software may not be called "Apache" | |||||
| * nor may "Apache" appear in their names without prior written | |||||
| * permission of the Apache Group. | |||||
| * | |||||
| * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
| * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
| * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
| * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
| * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
| * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
| * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
| * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
| * SUCH DAMAGE. | |||||
| * ==================================================================== | |||||
| * | |||||
| * This software consists of voluntary contributions made by many | |||||
| * individuals on behalf of the Apache Software Foundation. For more | |||||
| * information on the Apache Software Foundation, please see | |||||
| * <http://www.apache.org/>. | |||||
| */ | |||||
| package org.apache.tools.ant.util; | |||||
| /** | |||||
| * Implementation of FileNameMapper that always returns the same | |||||
| * target file name. | |||||
| * | |||||
| * <p>This is the default FileNameMapper for the archiving tasks and | |||||
| * uptodate.</p> | |||||
| * | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
| */ | |||||
| 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; | |||||
| } | |||||
| } | |||||
| @@ -87,7 +87,8 @@ public class SourceFileScanner { | |||||
| * | * | ||||
| * @param files the original set of files | * @param files the original set of files | ||||
| * @param srcDir all files are relative to this directory | * @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 | * @param mapper knows how to construct a target file names from | ||||
| * source file names. | * source file names. | ||||
| */ | */ | ||||
| @@ -116,7 +117,13 @@ public class SourceFileScanner { | |||||
| boolean added = false; | boolean added = false; | ||||
| targetList.setLength(0); | targetList.setLength(0); | ||||
| for (int j=0; !added && j<targets.length; j++) { | for (int j=0; !added && j<targets.length; j++) { | ||||
| File dest = new File(destDir, targets[j]); | |||||
| File dest = null; | |||||
| if (destDir == null) { | |||||
| dest = new File(targets[j]); | |||||
| } else { | |||||
| dest = new File(destDir, targets[j]); | |||||
| } | |||||
| if (!dest.exists()) { | if (!dest.exists()) { | ||||
| task.log(files[i]+" added as "+dest.getAbsolutePath()+" doesn\'t exist.", | task.log(files[i]+" added as "+dest.getAbsolutePath()+" doesn\'t exist.", | ||||
| Project.MSG_VERBOSE); | Project.MSG_VERBOSE); | ||||