From 647cd1207fd347edd66b16ececada6d8ecd303c8 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 5 Dec 2008 16:38:47 +0000 Subject: [PATCH] add a preservelastmodified attribute to replace and replaceregexp. PR 39002. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@723786 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++++ docs/manual/CoreTasks/replace.html | 6 ++++++ docs/manual/OptionalTasks/replaceregexp.html | 6 ++++++ .../taskdefs/optional/replaceregexp.xml | 14 ++++++++++++- src/etc/testcases/taskdefs/replace.xml | 12 +++++++++++ .../apache/tools/ant/taskdefs/Replace.java | 16 ++++++++++++++ .../ant/taskdefs/optional/ReplaceRegExp.java | 15 +++++++++++++ .../tools/ant/taskdefs/ReplaceTest.java | 21 ++++++++++++++++++- .../taskdefs/optional/ReplaceRegExpTest.java | 18 ++++++++++++++++ 9 files changed, 110 insertions(+), 2 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 335787d63..61d7401ff 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -607,6 +607,10 @@ Other changes: easily. Bugzilla Report 39568. + * and can now optionally preserve the file + timestamp even if the file is modified. + Bugzilla Report 39002. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/replace.html b/docs/manual/CoreTasks/replace.html index 0b0eba3ba..00715c746 100644 --- a/docs/manual/CoreTasks/replace.html +++ b/docs/manual/CoreTasks/replace.html @@ -120,6 +120,12 @@ have been regenerated by this task.

("yes"/"no"). Default excludes are used when omitted. No + + preserveLastModified + Keep the file timestamp(s) even if the file(s) + is(are) modified. + No, defaults to false +

Examples

  <replace file="${src}/index.html" token="@@@" value="wombat"/>
diff --git a/docs/manual/OptionalTasks/replaceregexp.html b/docs/manual/OptionalTasks/replaceregexp.html index 1eac27e4d..1b668a945 100644 --- a/docs/manual/OptionalTasks/replaceregexp.html +++ b/docs/manual/OptionalTasks/replaceregexp.html @@ -87,6 +87,12 @@ See details in the documentation of the The encoding of the file. since Ant 1.6 No - defaults to default JVM encoding + + preserveLastModified + Keep the file timestamp(s) even if the file(s) + is(are) modified. + No, defaults to false +

Examples

diff --git a/src/etc/testcases/taskdefs/optional/replaceregexp.xml b/src/etc/testcases/taskdefs/optional/replaceregexp.xml
index 878dd7c0c..3d4016865 100644
--- a/src/etc/testcases/taskdefs/optional/replaceregexp.xml
+++ b/src/etc/testcases/taskdefs/optional/replaceregexp.xml
@@ -16,7 +16,7 @@
   limitations under the License.
 -->
 
-  
+  
   
     This build file should only be run from within the testcase
   
@@ -62,6 +62,18 @@
     
   
 
+  
+    
+    Hello, world!
+  
+  
+    
+  
+  
+    
+  
+
   
     
     
diff --git a/src/etc/testcases/taskdefs/replace.xml b/src/etc/testcases/taskdefs/replace.xml
index a32160787..25abfc3d0 100644
--- a/src/etc/testcases/taskdefs/replace.xml
+++ b/src/etc/testcases/taskdefs/replace.xml
@@ -74,6 +74,18 @@
     
   
 
+  
+    
+    Hello, world!
+  
+  
+    
+  
+  
+    
+  
+
   
       
   
diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java
index 978541f7b..5bbc7f9fb 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Replace.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java
@@ -79,6 +79,8 @@ public class Replace extends MatchingTask {
 
     private Union resources;
 
+    private boolean preserveLastModified = false;
+
     /**
      * An inline string to use as the replacement text.
      */
@@ -666,7 +668,11 @@ public class Replace extends MatchingTask {
             boolean changes = (replaceCount != repCountStart);
             if (changes) {
                 fileCount++;
+                long origLastModified = src.lastModified();
                 FILE_UTILS.rename(temp, src);
+                if (preserveLastModified) {
+                    FILE_UTILS.setFileLastModified(src, origLastModified);
+                }
                 temp = null;
             }
         } catch (IOException ioe) {
@@ -862,6 +868,16 @@ public class Replace extends MatchingTask {
         resources.add(rc);
     }
 
+    /**
+     * Whether the file timestamp shall be preserved even if the file
+     * is modified.
+     *
+     * @since Ant 1.8.0
+     */
+    public void setPreserveLastModified(boolean b) {
+        preserveLastModified = b;
+    }
+
     /**
      * Adds the token and value as first <replacefilter> element.
      * The token and value are always processed first.
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
index bbe05b836..2895204fc 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
@@ -127,6 +127,8 @@ public class ReplaceRegExp extends Task {
 
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
 
+    private boolean preserveLastModified = false;
+
     /**
      * Encoding to assume for the files
      */
@@ -303,6 +305,15 @@ public class ReplaceRegExp extends Task {
         return subs;
     }
 
+    /**
+     * Whether the file timestamp shall be preserved even if the file
+     * is modified.
+     *
+     * @since Ant 1.8.0
+     */
+    public void setPreserveLastModified(boolean b) {
+        preserveLastModified = b;
+    }
 
     /**
      * Invoke a regular expression (r) on a string (input) using
@@ -460,7 +471,11 @@ public class ReplaceRegExp extends Task {
             if (changes) {
                 log("File has changed; saving the updated file", Project.MSG_VERBOSE);
                 try {
+                    long origLastModified = f.lastModified();
                     FILE_UTILS.rename(temp, f);
+                    if (preserveLastModified) {
+                        FILE_UTILS.setFileLastModified(f, origLastModified);
+                    }
                     temp = null;
                 } catch (IOException e) {
                     throw new BuildException("Couldn't rename temporary file "
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
index 22ee92a84..cf8cea714 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
@@ -68,12 +68,31 @@ public class ReplaceTest extends BuildFileTest {
         executeTarget("test8");
     }
 
-    public void test9() throws IOException{
+    public void test9() throws IOException {
         executeTarget("test9");
         String tmpdir = project.getProperty("tmp.dir");
         assertEqualContent(new File(tmpdir, "result.txt"),
                     new File(tmpdir, "output.txt"));
     }
+
+    public void testNoPreserveLastModified() throws Exception {
+        executeTarget("lastModifiedSetup");
+        String tmpdir = project.getProperty("tmp.dir");
+        long ts1 = new File(tmpdir, "test.txt").lastModified();
+        Thread.sleep(2);
+        executeTarget("testNoPreserve");
+        assertTrue(ts1 < new File(tmpdir, "test.txt").lastModified());
+    }
+
+    public void testPreserveLastModified() throws Exception {
+        executeTarget("lastModifiedSetup");
+        String tmpdir = project.getProperty("tmp.dir");
+        long ts1 = new File(tmpdir, "test.txt").lastModified();
+        Thread.sleep(2);
+        executeTarget("testPreserve");
+        assertTrue(ts1 == new File(tmpdir, "test.txt").lastModified());
+    }
+
     public void tearDown() {
         executeTarget("cleanup");
     }
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
index bc18d05cf..42bafedaa 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
@@ -102,4 +102,22 @@ public class ReplaceRegExpTest extends BuildFileTest {
                                   new File(System.getProperty("root"), PROJECT_PATH + "/replaceregexp2.result.properties")));
     }
 
+    public void testNoPreserveLastModified() throws Exception {
+        executeTarget("lastModifiedSetup");
+        String tmpdir = project.getProperty("tmpregexp");
+        long ts1 = new File(tmpdir, "test.txt").lastModified();
+        Thread.sleep(2);
+        executeTarget("testNoPreserve");
+        assertTrue(ts1 < new File(tmpdir, "test.txt").lastModified());
+    }
+
+    public void testPreserveLastModified() throws Exception {
+        executeTarget("lastModifiedSetup");
+        String tmpdir = project.getProperty("tmpregexp");
+        long ts1 = new File(tmpdir, "test.txt").lastModified();
+        Thread.sleep(2);
+        executeTarget("testPreserve");
+        assertTrue(ts1 == new File(tmpdir, "test.txt").lastModified());
+    }
+
 }// ReplaceRegExpTest