git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@723786 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -607,6 +607,10 @@ Other changes: | |||||
| easily. | easily. | ||||
| Bugzilla Report 39568. | 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 | Changes from Ant 1.7.0 TO Ant 1.7.1 | ||||
| ============================================= | ============================================= | ||||
| @@ -120,6 +120,12 @@ have been regenerated by this task.</p> | |||||
| ("yes"/"no"). Default excludes are used when omitted.</td> | ("yes"/"no"). Default excludes are used when omitted.</td> | ||||
| <td valign="top" align="center">No</td> | <td valign="top" align="center">No</td> | ||||
| </tr> | </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> | </table> | ||||
| <h3>Examples</h3> | <h3>Examples</h3> | ||||
| <pre> <replace file="${src}/index.html" token="@@@" value="wombat"/></pre> | <pre> <replace file="${src}/index.html" token="@@@" value="wombat"/></pre> | ||||
| @@ -87,6 +87,12 @@ See details in the documentation of the <a href="../CoreTypes/regexp.html#implem | |||||
| <td valign="top">The encoding of the file. <em>since Ant 1.6</em></td> | <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> | <td align="center">No - defaults to default JVM encoding</td> | ||||
| </tr> | </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> | </table> | ||||
| <h3>Examples</h3> | <h3>Examples</h3> | ||||
| <pre> | <pre> | ||||
| @@ -16,7 +16,7 @@ | |||||
| limitations under the License. | limitations under the License. | ||||
| --> | --> | ||||
| <project name="test" default="def" basedir="."> | <project name="test" default="def" basedir="."> | ||||
| <property name="tmpregexp" value="tmpregexp"/> | |||||
| <property name="tmpregexp" location="tmpregexp"/> | |||||
| <target name="def"> | <target name="def"> | ||||
| <fail>This build file should only be run from within the testcase</fail> | <fail>This build file should only be run from within the testcase</fail> | ||||
| </target> | </target> | ||||
| @@ -62,6 +62,18 @@ | |||||
| </replaceregexp> | </replaceregexp> | ||||
| </target> | </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"> | <target name="cleanup"> | ||||
| <delete file="test.properties" /> | <delete file="test.properties" /> | ||||
| <delete dir="${tmpregexp}" quiet="true"/> | <delete dir="${tmpregexp}" quiet="true"/> | ||||
| @@ -74,6 +74,18 @@ | |||||
| <replace file="${tmp.dir}/output.txt" token="@@@Replace this@@@" value="${content}"/> | <replace file="${tmp.dir}/output.txt" token="@@@Replace this@@@" value="${content}"/> | ||||
| </target> | </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"> | <target name="cleanup"> | ||||
| <delete dir="${tmp.dir}" quiet="true"/> | <delete dir="${tmp.dir}" quiet="true"/> | ||||
| </target> | </target> | ||||
| @@ -79,6 +79,8 @@ public class Replace extends MatchingTask { | |||||
| private Union resources; | private Union resources; | ||||
| private boolean preserveLastModified = false; | |||||
| /** | /** | ||||
| * An inline string to use as the replacement text. | * An inline string to use as the replacement text. | ||||
| */ | */ | ||||
| @@ -666,7 +668,11 @@ public class Replace extends MatchingTask { | |||||
| boolean changes = (replaceCount != repCountStart); | boolean changes = (replaceCount != repCountStart); | ||||
| if (changes) { | if (changes) { | ||||
| fileCount++; | fileCount++; | ||||
| long origLastModified = src.lastModified(); | |||||
| FILE_UTILS.rename(temp, src); | FILE_UTILS.rename(temp, src); | ||||
| if (preserveLastModified) { | |||||
| FILE_UTILS.setFileLastModified(src, origLastModified); | |||||
| } | |||||
| temp = null; | temp = null; | ||||
| } | } | ||||
| } catch (IOException ioe) { | } catch (IOException ioe) { | ||||
| @@ -862,6 +868,16 @@ public class Replace extends MatchingTask { | |||||
| resources.add(rc); | 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. | * Adds the token and value as first <replacefilter> element. | ||||
| * The token and value are always processed first. | * The token and value are always processed first. | ||||
| @@ -127,6 +127,8 @@ public class ReplaceRegExp extends Task { | |||||
| private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | ||||
| private boolean preserveLastModified = false; | |||||
| /** | /** | ||||
| * Encoding to assume for the files | * Encoding to assume for the files | ||||
| */ | */ | ||||
| @@ -303,6 +305,15 @@ public class ReplaceRegExp extends Task { | |||||
| return subs; | 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 | * Invoke a regular expression (r) on a string (input) using | ||||
| @@ -460,7 +471,11 @@ public class ReplaceRegExp extends Task { | |||||
| if (changes) { | if (changes) { | ||||
| log("File has changed; saving the updated file", Project.MSG_VERBOSE); | log("File has changed; saving the updated file", Project.MSG_VERBOSE); | ||||
| try { | try { | ||||
| long origLastModified = f.lastModified(); | |||||
| FILE_UTILS.rename(temp, f); | FILE_UTILS.rename(temp, f); | ||||
| if (preserveLastModified) { | |||||
| FILE_UTILS.setFileLastModified(f, origLastModified); | |||||
| } | |||||
| temp = null; | temp = null; | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| throw new BuildException("Couldn't rename temporary file " | throw new BuildException("Couldn't rename temporary file " | ||||
| @@ -68,12 +68,31 @@ public class ReplaceTest extends BuildFileTest { | |||||
| executeTarget("test8"); | executeTarget("test8"); | ||||
| } | } | ||||
| public void test9() throws IOException{ | |||||
| public void test9() throws IOException { | |||||
| executeTarget("test9"); | executeTarget("test9"); | ||||
| String tmpdir = project.getProperty("tmp.dir"); | String tmpdir = project.getProperty("tmp.dir"); | ||||
| assertEqualContent(new File(tmpdir, "result.txt"), | assertEqualContent(new File(tmpdir, "result.txt"), | ||||
| new File(tmpdir, "output.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() { | public void tearDown() { | ||||
| executeTarget("cleanup"); | executeTarget("cleanup"); | ||||
| } | } | ||||
| @@ -102,4 +102,22 @@ public class ReplaceRegExpTest extends BuildFileTest { | |||||
| new File(System.getProperty("root"), PROJECT_PATH + "/replaceregexp2.result.properties"))); | 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 | }// ReplaceRegExpTest | ||||