Browse Source

add a failOnNoReplacements attribute to replace. PR 21064.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@724377 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
fe67f95603
4 changed files with 38 additions and 0 deletions
  1. +4
    -0
      WHATSNEW
  2. +6
    -0
      docs/manual/CoreTasks/replace.html
  3. +13
    -0
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  4. +15
    -0
      src/tests/antunit/taskdefs/replace-test.xml

+ 4
- 0
WHATSNEW View File

@@ -616,6 +616,10 @@ Other changes:
expanded. expanded.
Bugzilla Report 11585. Bugzilla Report 11585.


* <replace> has a new attribute failOnNoReplacements that makes the
build fail if the task didn't do anything.
Bugzilla Report 21064.

Changes from Ant 1.7.0 TO Ant 1.7.1 Changes from Ant 1.7.0 TO Ant 1.7.1
============================================= =============================================




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

@@ -126,6 +126,12 @@ have been regenerated by this task.</p>
is(are) modified. <em>since Ant 1.8.0.</em></td> is(are) modified. <em>since Ant 1.8.0.</em></td>
<td valign="top" align="center">No, defaults to false</td> <td valign="top" align="center">No, defaults to false</td>
</tr> </tr>
<tr>
<td valign="top">failOnNoReplacements</td>
<td valign="top">Whether to fail the build if the task didn't do
anything. <em>since Ant 1.8.0.</em></td>
<td valign="top" align="center">No, defaults to false</td>
</tr>
</table> </table>
<h3>Examples</h3> <h3>Examples</h3>
<pre> &lt;replace file=&quot;${src}/index.html&quot; token=&quot;@@@&quot; value=&quot;wombat&quot;/&gt;</pre> <pre> &lt;replace file=&quot;${src}/index.html&quot; token=&quot;@@@&quot; value=&quot;wombat&quot;/&gt;</pre>


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

@@ -80,6 +80,7 @@ public class Replace extends MatchingTask {
private Union resources; private Union resources;


private boolean preserveLastModified = false; private boolean preserveLastModified = false;
private boolean failOnNoReplacements = false;


/** /**
* An inline string to use as the replacement text. * An inline string to use as the replacement text.
@@ -559,6 +560,9 @@ public class Replace extends MatchingTask {
log("Replaced " + replaceCount + " occurrences in " log("Replaced " + replaceCount + " occurrences in "
+ fileCount + " files.", Project.MSG_INFO); + fileCount + " files.", Project.MSG_INFO);
} }
if (failOnNoReplacements && replaceCount == 0) {
throw new BuildException("didn't replace anything");
}
} finally { } finally {
replacefilters = savedFilters; replacefilters = savedFilters;
properties = savedProperties; properties = savedProperties;
@@ -894,6 +898,15 @@ public class Replace extends MatchingTask {
preserveLastModified = b; preserveLastModified = b;
} }


/**
* Whether the build should fail if nothing has been replaced.
*
* @since Ant 1.8.0
*/
public void setFailOnNoReplacements(boolean b) {
failOnNoReplacements = b;
}

/** /**
* Adds the token and value as first &lt;replacefilter&gt; element. * Adds the token and value as first &lt;replacefilter&gt; element.
* The token and value are always processed first. * The token and value are always processed first.


+ 15
- 0
src/tests/antunit/taskdefs/replace-test.xml View File

@@ -67,4 +67,19 @@ Hello, world!
resource="${output}/text.txt" value="Hello, Ant!"/> resource="${output}/text.txt" value="Hello, Ant!"/>
</target> </target>


<target name="testNoReplace" depends="setUp">
<replace token="ant" value="ant" summary="true">
<file file="${output}/text.txt"/>
</replace>
<au:assertLogContains text="Replaced 0 occurrences in 0 files."/>
</target>

<target name="testFailOnNoReplace" depends="setUp">
<au:expectfailure expectedMessage="didn't replace anything">
<replace token="ant" value="ant" failOnNoReplacements="true">
<file file="${output}/text.txt"/>
</replace>
</au:expectfailure>
</target>

</project> </project>

Loading…
Cancel
Save