From f14a2ef8c36b9de729a3715f9f7ee3d74d837ed0 Mon Sep 17 00:00:00 2001 From: Jacobus Martinus Kruithof Date: Sun, 2 Sep 2007 12:53:16 +0000 Subject: [PATCH] Improvement of handling mappers in the touch task. datetime and millis now take precedence over the timestamp on the original file. Bugzilla report 43235. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@571970 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++ docs/manual/CoreTasks/touch.html | 21 ++++-- .../org/apache/tools/ant/taskdefs/Touch.java | 64 ++++++++++--------- 3 files changed, 56 insertions(+), 33 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 3928772de..93d3909c7 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -21,6 +21,10 @@ Changes that could break older environments: in Ant 1.7.0 has been removed. Bugzilla report 40511. +* In the task when a is used, the millis and datetime + attributes now override the time of the source resource if provisioned. + Bugzilla report 43235. + Fixed bugs: ----------- diff --git a/docs/manual/CoreTasks/touch.html b/docs/manual/CoreTasks/touch.html index 3f8198ba6..a3fde3b4a 100644 --- a/docs/manual/CoreTasks/touch.html +++ b/docs/manual/CoreTasks/touch.html @@ -56,7 +56,9 @@ resource collections (which also includes directories). Prior to Ant datetime - Specifies the new modification time of the file. + Specifies the new modification time of the file. The + special value "now" indicates the current time + (now supported since Ant 1.8). pattern @@ -99,9 +101,12 @@ collection.

mapper can be specified. Files specified via nested filesets, filelists, or the file attribute are mapped using the specified mapper. For each file mapped, - the resulting files are touched. If the original file exists its - timestamp will be used. Otherwise the task settings (millis, - datetime) take effect.

+ the resulting files are touched. If no time has been specified and + the original file exists its timestamp will be used. + If no time has been specified and the original file does not exist the + current time is used. Since Ant 1.8 the task settings (millis, + and datetime) have priority over the timestamp of the original + file.

Examples

  <touch file="myfile"/>

creates myfile if it doesn't exist and changes the @@ -127,6 +132,14 @@ time close to it otherwise.

creates bar if it doesn't exist and changes the modification time to that of foo.

+
  <touch file="foo" datetime="now">
+    <mapper type="regexp" from="^src(.*)\.java" to="shadow\1.empty" />
+  </touch>
+
+

creates files in the shadow directory for every java file in the + src directory if it doesn't exist and changes the modification + time of those files to the current time.

+ diff --git a/src/main/org/apache/tools/ant/taskdefs/Touch.java b/src/main/org/apache/tools/ant/taskdefs/Touch.java index 61a3ac2d7..5547376d0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Touch.java +++ b/src/main/org/apache/tools/ant/taskdefs/Touch.java @@ -231,44 +231,48 @@ public class Touch extends Task { } if (dateTime != null && !dateTimeConfigured) { long workmillis = millis; - DateFormat df = dfFactory.getPrimaryFormat(); - ParseException pe = null; - try { - workmillis = df.parse(dateTime).getTime(); - } catch (ParseException peOne) { - df = dfFactory.getFallbackFormat(); - if (df == null) { - pe = peOne; - } else { - try { - workmillis = df.parse(dateTime).getTime(); - } catch (ParseException peTwo) { - pe = peTwo; + if ("now".equalsIgnoreCase(dateTime)) { + workmillis = System.currentTimeMillis(); + } else { + DateFormat df = dfFactory.getPrimaryFormat(); + ParseException pe = null; + try { + workmillis = df.parse(dateTime).getTime(); + } catch (ParseException peOne) { + df = dfFactory.getFallbackFormat(); + if (df == null) { + pe = peOne; + } else { + try { + workmillis = df.parse(dateTime).getTime(); + } catch (ParseException peTwo) { + pe = peTwo; + } } } - } - if (pe != null) { - throw new BuildException(pe.getMessage(), pe, getLocation()); - } - if (workmillis < 0) { - throw new BuildException("Date of " + dateTime - + " results in negative " - + "milliseconds value " - + "relative to epoch " - + "(January 1, 1970, " - + "00:00:00 GMT)."); + if (pe != null) { + throw new BuildException(pe.getMessage(), pe, getLocation()); + } + if (workmillis < 0) { + throw new BuildException("Date of " + dateTime + + " results in negative " + "milliseconds value " + + "relative to epoch " + "(January 1, 1970, " + + "00:00:00 GMT)."); + } } log("Setting millis to " + workmillis + " from datetime attribute", - ((millis < 0) ? Project.MSG_DEBUG : Project.MSG_VERBOSE)); + ((millis < 0) ? Project.MSG_DEBUG : Project.MSG_VERBOSE)); setMillis(workmillis); - //only set if successful to this point: + // only set if successful to this point: dateTimeConfigured = true; } } /** * Execute the touch operation. - * @throws BuildException if an error occurs. + * + * @throws BuildException + * if an error occurs. */ public void execute() throws BuildException { checkConfiguration(); @@ -339,8 +343,10 @@ public class Touch extends Task { } else { String[] mapped = fileNameMapper.mapFileName(r.getName()); if (mapped != null && mapped.length > 0) { - long modTime = (r.isExists()) ? r.getLastModified() - : defaultTimestamp; + long modTime = defaultTimestamp; + if (millis < 0 && r.isExists()){ + modTime = r.getLastModified(); + } for (int i = 0; i < mapped.length; i++) { touch(getProject().resolveFile(mapped[i]), modTime); }