Replace Task

## Replace

### Description

`Replace` is a directory based task for replacing the occurrence of a given string with another string in selected file.

If you want to replace a text that crosses line boundaries, you must use a nested `<replacetoken>` element.

The output file is only written if it differs from the existing file. This prevents spurious rebuilds based on unchanged files which have been regenerated by this task.

### Parameters

Attribute | Description | Required
--- | --- | ---
file | file for which the token should be replaced. | Exactly one of the two
dir | The base directory to use when replacing a token in multiple files.
encoding | The encoding of the files upon which replace operates. | No; defaults to default JVM character encoding
token | the token which must be replaced. | Yes, unless a nested `replacetoken` element or the replacefilterfile attribute is used.
value | the new value for the token. | No; defaults to empty string ()
summary | Indicates whether a summary of the replace operation should be produced, detailing how many token occurrences and files were processed | No; defaults to no summary
propertyFile | valid property file from which properties specified using nested `<replacefilter>` elements are drawn. | Yes, only if property attribute of `<replacefilter>` is used.
replacefilterfile | valid property file. Each property will be treated as a `replacefilter` where token is the name of the property and value is the property's value. | No
includes | comma- or space-separated list of patterns of files that must be included. | No; defaults to all (**)
includesfile | name of a file. Each line of this file is taken to be an include pattern | No
excludes | comma- or space-separated list of patterns of files that must be excluded. | No; defaults to default excludes or none if defaultexcludes is no
excludesfile | name of a file. Each line of this file is taken to be an exclude pattern | No
defaultexcludes | indicates whether default excludes should be used or not (yes|no). | No; defaults to yes
preserveLastModified | Keep the file timestamp(s) even if the file(s) is(are) modified. since Apache Ant 1.8.0. | No; defaults to false
failOnNoReplacements | Whether to fail the build if the task didn't do anything. since Ant 1.8.0. | No; defaults to false

### Examples

    <replace file="${src}/index.html" token="@@@" value="wombat"/>

replaces occurrences of the string @@@ with the string wombat, in the file `${src}/index.html`.

### Parameters specified as nested elements

This task forms an implicit FileSet and supports most attributes of `<fileset>` as well as the nested `<include>`, `<exclude>` and `<patternset>` elements.

Since Ant 1.8.0, this task supports any filesystem based resource collections as nested elements.

#### replacetoken and replacevalue

If either the text you want to replace or the replacement text cross line boundaries, you can use nested elements to specify them.

The elements support attributes:

Attribute | Description | Required
--- | --- | ---
expandProperties | Whether to expand properties in the nested text. Since Ant 1.8.0. | No; defaults to true

### Examples

    <replace dir="${src}" value="wombat">
      <include name="**/*.html"/>
      <replacetoken><![CDATA[multi line
    token]]></replacetoken>
    </replace>

replaces occurrences of the string multi line\ntoken with the string wombat, in all HTML files in the directory `${src}`. Here, \n is the platform specific line separator.

    <replace file="${src}/index.html">
      <replacetoken><![CDATA[two line
    token]]></replacetoken>
      <replacevalue><![CDATA[two line
    token]]></replacevalue>
    </replace>

#### replacefilter

In addition to allowing for multiple replacements, optional nested `<replacefilter>` elements allow replacement values to be extracted from a property file. The name of this file is specified using the `<replace>` attribute propertyFile.

Attribute | Description | Required
--- | --- | ---
token | The string to search for. | Yes, unless a nested `replacetoken` is specified
value | The replacement string. | Either may be specified, but not both. Both can be omitted, if desired.
property | Name of the property whose value is to serve as the replacement value.

Since Ant 1.8.0, token and value can be specified as nested elements just like in the task itself.

If neither value nor property is used, the value provided using the `<replace>` attribute value and/or the `<replacevalue>` element is used. If no value was specified using either of these options, the token is replaced with an empty string.

### Examples

    <replace file="configure.sh"
             value="defaultvalue"
             propertyFile="src/name.properties">
      <replacefilter token="@token1@"/>
      <replacefilter token="@token2@"
                     value="value2"/>
      <replacefilter token="@token3@"
                     property="property.key"/>
      <replacefilter>
        <replacetoken>@token4@</replacetoken>
        <replacevalue>value4</replacevalue>
      </replacefilter>
    </replace>

In file `configure.sh`, replace all instances of @token1@ with defaultvalue, all instances of @token2@ with value2, and all instances of @token3@ with the value of the property `property.key`, as it appears in property file `src/name.properties`.

Note: It is possible to use either the token/`<replacetoken>` and value/`<replacevalue>` attributes/elements, the nested `replacefilter` elements, or both in the same operation.