ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r723786 - in /ant/core/trunk: ./ docs/manual/CoreTasks/ docs/manual/OptionalTasks/ src/etc/testcases/taskdefs/ src/etc/testcases/taskdefs/optional/ src/main/org/apache/tools/ant/taskdefs/ src/main/org/apache/tools/ant/taskdefs/optional/ src...
Date Fri, 05 Dec 2008 16:38:48 GMT
Author: bodewig
Date: Fri Dec  5 08:38:47 2008
New Revision: 723786

URL: http://svn.apache.org/viewvc?rev=723786&view=rev
Log:
add a preservelastmodified attribute to replace and replaceregexp.  PR 39002.

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/replace.html
    ant/core/trunk/docs/manual/OptionalTasks/replaceregexp.html
    ant/core/trunk/src/etc/testcases/taskdefs/optional/replaceregexp.xml
    ant/core/trunk/src/etc/testcases/taskdefs/replace.xml
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Fri Dec  5 08:38:47 2008
@@ -607,6 +607,10 @@
    easily.
    Bugzilla Report 39568.
 
+ * <replace> and <replaceregexp> 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
 =============================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/replace.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/replace.html?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/replace.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/replace.html Fri Dec  5 08:38:47 2008
@@ -120,6 +120,12 @@
       (&quot;yes&quot;/&quot;no&quot;). Default excludes are used when omitted.</td>
     <td valign="top" align="center">No</td>
   </tr>
+  <tr>
+    <td valign="top">preserveLastModified</td>
+    <td valign="top">Keep the file timestamp(s) even if the file(s)
+      is(are) modified.</td>
+    <td valign="top" align="center">No, defaults to false</td>
+  </tr>
 </table>
 <h3>Examples</h3>
 <pre>  &lt;replace file=&quot;${src}/index.html&quot; token=&quot;@@@&quot;
value=&quot;wombat&quot;/&gt;</pre>

Modified: ant/core/trunk/docs/manual/OptionalTasks/replaceregexp.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/OptionalTasks/replaceregexp.html?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/OptionalTasks/replaceregexp.html (original)
+++ ant/core/trunk/docs/manual/OptionalTasks/replaceregexp.html Fri Dec  5 08:38:47 2008
@@ -87,6 +87,12 @@
     <td valign="top">The encoding of the file. <em>since Ant 1.6</em></td>
     <td align="center">No - defaults to default JVM encoding</td>
   </tr>
+  <tr>
+    <td valign="top">preserveLastModified</td>
+    <td valign="top">Keep the file timestamp(s) even if the file(s)
+      is(are) modified.</td>
+    <td valign="top" align="center">No, defaults to false</td>
+  </tr>
 </table>
 <h3>Examples</h3>
 <pre>

Modified: ant/core/trunk/src/etc/testcases/taskdefs/optional/replaceregexp.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/etc/testcases/taskdefs/optional/replaceregexp.xml?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/src/etc/testcases/taskdefs/optional/replaceregexp.xml (original)
+++ ant/core/trunk/src/etc/testcases/taskdefs/optional/replaceregexp.xml Fri Dec  5 08:38:47
2008
@@ -16,7 +16,7 @@
   limitations under the License.
 -->
 <project name="test" default="def" basedir=".">
-  <property name="tmpregexp" value="tmpregexp"/>
+  <property name="tmpregexp" location="tmpregexp"/>
   <target name="def">
     <fail>This build file should only be run from within the testcase</fail>
   </target>
@@ -62,6 +62,18 @@
     </replaceregexp>
   </target>
 
+  <target name="lastModifiedSetup">
+    <mkdir dir="${tmpregexp}"/>
+    <echo file="${tmpregexp}/test.txt">Hello, world!</echo>
+  </target>
+  <target name="testNoPreserve">
+    <replaceregexp match="world" replace="Ant" file="${tmpregexp}/test.txt"/>
+  </target>
+  <target name="testPreserve">
+    <replaceregexp match="world" replace="Ant" file="${tmpregexp}/test.txt"
+                   preserveLastModified="true"/>
+  </target>
+
   <target name="cleanup">
     <delete file="test.properties" />
     <delete dir="${tmpregexp}" quiet="true"/>

Modified: ant/core/trunk/src/etc/testcases/taskdefs/replace.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/etc/testcases/taskdefs/replace.xml?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/src/etc/testcases/taskdefs/replace.xml (original)
+++ ant/core/trunk/src/etc/testcases/taskdefs/replace.xml Fri Dec  5 08:38:47 2008
@@ -74,6 +74,18 @@
     <replace file="${tmp.dir}/output.txt" token="@@@Replace this@@@" value="${content}"/>
   </target>
 
+  <target name="lastModifiedSetup">
+    <mkdir dir="${tmp.dir}"/>
+    <echo file="${tmp.dir}/test.txt">Hello, world!</echo>
+  </target>
+  <target name="testNoPreserve">
+    <replace token="world" value="Ant" file="${tmp.dir}/test.txt"/>
+  </target>
+  <target name="testPreserve">
+    <replace token="world" value="Ant" file="${tmp.dir}/test.txt"
+             preserveLastModified="true"/>
+  </target>
+
   <target name="cleanup">
       <delete dir="${tmp.dir}" quiet="true"/>
   </target>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java Fri Dec  5 08:38:47
2008
@@ -79,6 +79,8 @@
 
     private Union resources;
 
+    private boolean preserveLastModified = false;
+
     /**
      * An inline string to use as the replacement text.
      */
@@ -666,7 +668,11 @@
             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) {
@@ -863,6 +869,16 @@
     }
 
     /**
+     * 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 &lt;replacefilter&gt; element.
      * The token and value are always processed first.
      * @return a nested <code>Replacefilter</code> object to be configured.

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java Fri
Dec  5 08:38:47 2008
@@ -127,6 +127,8 @@
 
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
 
+    private boolean preserveLastModified = false;
+
     /**
      * Encoding to assume for the files
      */
@@ -303,6 +305,15 @@
         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 @@
             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 "

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java (original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java Fri Dec
 5 08:38:47 2008
@@ -68,12 +68,31 @@
         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");
     }

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java?rev=723786&r1=723785&r2=723786&view=diff
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
(original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java
Fri Dec  5 08:38:47 2008
@@ -102,4 +102,22 @@
                                   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



Mime
View raw message