Hi, I wrote a new task named "UpToDate", inspired by "Available".

This task sets a property if a destination file is up to date,
that is, it has newer last modified time than source files.

With combination to another target's unless attribute, you can
run tasks unless the destination file is up to date.

Please see the attached files for details.

I think this task is fundamental enough, so please add this
UpToDate task to built-ins.

Thank you.

-- 
)Hiroaki Nakamura) [EMAIL PROTECTED]
--- defaults.properties.orig    Thu Sep 07 11:09:04 2000
+++ defaults.properties Sat Sep 09 17:26:16 2000
@@ -42,6 +42,7 @@
 sql=org.apache.tools.ant.taskdefs.SQLExec
 mail=org.apache.tools.ant.taskdefs.SendEmail
 fail=org.apache.tools.ant.taskdefs.Exit
+uptodate=org.apache.tools.ant.taskdefs.UpToDate
 
 # optional tasks
 script=org.apache.tools.ant.taskdefs.optional.Script
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 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 [EMAIL PROTECTED]
 *
 * 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.taskdefs;

import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;

import java.io.*;
import java.util.*;

/**
 * Task to check a destination file is up to date compared to source files.
 * The property is set to true if it is up to date.
 * arguments:
 * <ul>
 * <li>srcdir
 * <li>destfile
 * <li>property
 * </ul>
 * All of these arguments are required.
 * <p>
 *
 * @author Hiroaki Nakamura <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
 */

public class UpToDate extends MatchingTask {

    private Path src;
    private File destFile;
    private String property;

    /**
     * Create a nested <src ...> element for multiple source path
     * support.
     *
     * @return a nexted src element.
     */
    public Path createSrc() {
        if (src != null) {
            src = new Path(project);
        }
        return src;
    }

    /**
     * Set the source dirs to find the source Java files.
     */
    public void setSrcdir(Path srcDir) {
        if (src == null) {
            src = srcDir;
        } else {
            src.append(srcDir);
        }
    }

    /**
     * Set the destination file whose last modified date is compared
     * to those of source files.
     */
    public void setDestfile(File DestFile) {
        this.destFile = DestFile;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * Executes the task.
     */
    public void execute() throws BuildException {
        // first off, make sure that we've got a srcdir and destdir

        if (src == null) {
            throw new BuildException("srcdir attribute must be set!");
        }
        
        String [] list = src.list();
        if (list.length == 0) {
            throw new BuildException("srcdir attribute must be set!");
        }
        
        if (destFile == null) {
            throw new BuildException("destfile attribute must be set!");
        }

        if (!destFile.exists()) {
            log("File \"" + destFile.getPath() + "\" does not exist.",
                    Project.MSG_INFO);
            return;
        }

        if (property == null) {
            throw new BuildException("property attribute must be set!");
        }

        // scan source directories build up both copy lists and
        // compile lists
        for (int i=0; i<list.length; i++) {
            File srcDir = (File)project.resolveFile(list[i]);
            if (!srcDir.exists()) {
                throw new BuildException("srcdir " + srcDir.getPath()
                        + " does not exist!");
            }

            DirectoryScanner ds = this.getDirectoryScanner(srcDir);

            String[] files = ds.getIncludedFiles();

            boolean upToDate = scanDir(srcDir, destFile, files);
            if (upToDate) {
                this.project.setProperty(property, "true");
                log("File \"" + destFile.getPath() + "\" is up to date.",
                        Project.MSG_INFO);
            }
        }
    }

    /**
     * Scans the directory looking for source files to be compiled and
     * support files to be copied.  The results are returned in the
     * class variables compileList and filecopyList.
     */

    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;
    }
}
--- index.html.orig Thu Sep 07 11:09:00 2000 +++ index.html Sat Sep 09 18:52:22 2000 @@ -863,6 +863,7 @@
  • Unjar
  • Untar
  • Unzip
  • +
  • UpToDate
  • Zip

  • @@ -3740,6 +3741,59 @@ <untar src="" dest="${tools.home}"/>

    +
    +

    UpToDate

    +

    Description

    +

    Set a property if a destination file is up to date, that is, it has newer last modified time than source files.

    +

    Parameters

    + + + + + + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    destfilethe file to check which is up to date.Yes
    srcdirthe directory in which files are compared to the destfile.Yes
    propertythe name of property which is set when the destfile is up to date.Yes
    +

    Examples

    +
    +  <target name="check">
    +    <uptodate destfile="hello.jar" srcdir="." property="up_to_date">
    +      <include name="*.java" />
    +    </uptodate>
    +  </target>
    +
    +  <target name="build" depends="check" unless="up_to_date">
    +    <mkdir dir="work" />
    +    <javac srcdir="."
    +           destdir="work" />
    +    <jar jarfile="hello.jar" basedir="work" />
    +  </target>
    +
    +

    Tasks in the "build" target are executed only when the destination file +is not up to date. Without the "check" target and the "unless" attribute, +tasks in the "build" target are executed every time you run the "build" +target. In the above example, after you run the "build" target for the +first time, the tasks in the "build" target are executed but they do +nothing if you keep the "work" directory and the class file in it and +if the jar file is up to date. But if you delete the "work" directory, +those tasks do their works again.

    +

    With the "check" target and the "unless" attribute, you can skip the whole +"build" target. This is especially useful if you have more time-consuming +tasks in the "build" target.


    Zip

    Description

    
    
    

    Reply via email to