Browse Source

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
master
Stefan Bodewig 16 years ago
parent
commit
647cd1207f
9 changed files with 110 additions and 2 deletions
  1. +4
    -0
      WHATSNEW
  2. +6
    -0
      docs/manual/CoreTasks/replace.html
  3. +6
    -0
      docs/manual/OptionalTasks/replaceregexp.html
  4. +13
    -1
      src/etc/testcases/taskdefs/optional/replaceregexp.xml
  5. +12
    -0
      src/etc/testcases/taskdefs/replace.xml
  6. +16
    -0
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  7. +15
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
  8. +20
    -1
      src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java
  9. +18
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java

+ 4
- 0
WHATSNEW View File

@@ -607,6 +607,10 @@ Other changes:
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
=============================================



+ 6
- 0
docs/manual/CoreTasks/replace.html View File

@@ -120,6 +120,12 @@ have been regenerated by this task.</p>
(&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>


+ 6
- 0
docs/manual/OptionalTasks/replaceregexp.html View File

@@ -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 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>


+ 13
- 1
src/etc/testcases/taskdefs/optional/replaceregexp.xml View File

@@ -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"/>


+ 12
- 0
src/etc/testcases/taskdefs/replace.xml View File

@@ -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>


+ 16
- 0
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -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 &lt;replacefilter&gt; element.
* The token and value are always processed first.


+ 15
- 0
src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java View File

@@ -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 "


+ 20
- 1
src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java View File

@@ -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");
}


+ 18
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/optional/ReplaceRegExpTest.java View File

@@ -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

Loading…
Cancel
Save