You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

replace.html 8.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Replace Task</title>
  20. </head>
  21. <body>
  22. <h2 id="replace">Replace</h2>
  23. <h3>Description</h3>
  24. <p><code>Replace</code> is a directory based task for replacing the occurrence of a given string
  25. with another string in selected file.</p>
  26. <p>If you want to replace a text that crosses line boundaries, you must use a
  27. nested <code>&lt;replacetoken&gt;</code> element.</p>
  28. <p>The output file is only written if it differs from the existing file. This prevents spurious
  29. rebuilds based on unchanged files which have been regenerated by this task.</p>
  30. <h3>Parameters</h3>
  31. <table class="attr">
  32. <tr>
  33. <th scope="col">Attribute</th>
  34. <th scope="col">Description</th>
  35. <th scope="col">Required</th>
  36. </tr>
  37. <tr>
  38. <td>file</td>
  39. <td>file for which the token should be replaced.</td>
  40. <td rowspan="2">Exactly one of the two</td>
  41. </tr>
  42. <tr>
  43. <td>dir</td>
  44. <td class="left">The base directory to use when replacing a token in multiple files.</td>
  45. </tr>
  46. <tr>
  47. <td>encoding</td>
  48. <td>The encoding of the files upon which replace operates.</td>
  49. <td>No; defaults to default JVM character encoding</td>
  50. </tr>
  51. <tr>
  52. <td>token</td>
  53. <td>the token which must be replaced.</td>
  54. <td>Yes, unless a nested <code>replacetoken</code> element or the <var>replacefilterfile</var>
  55. attribute is used.</td>
  56. </tr>
  57. <tr>
  58. <td>value</td>
  59. <td>the new value for the token.</td>
  60. <td>No; defaults to empty string (<q></q>)</td>
  61. </tr>
  62. <tr>
  63. <td>summary</td>
  64. <td>Indicates whether a summary of the replace operation should be produced, detailing how many
  65. token occurrences and files were processed</td>
  66. <td>No; defaults to no summary</td>
  67. </tr>
  68. <tr>
  69. <td>propertyFile</td>
  70. <td>valid property file from which properties specified using
  71. nested <code>&lt;replacefilter&gt;</code> elements are drawn.</td>
  72. <td>Yes, only if <var>property</var> attribute of <code>&lt;replacefilter&gt;</code> is
  73. used.</td>
  74. </tr>
  75. <tr>
  76. <td>replacefilterfile</td>
  77. <td>valid property file. Each property will be treated as a <code>replacefilter</code>
  78. where <var>token</var> is the name of the property and <var>value</var> is the property's
  79. value.</td>
  80. <td>No</td>
  81. </tr>
  82. <tr>
  83. <td>includes</td>
  84. <td>comma- or space-separated list of patterns of files that must be included.</td>
  85. <td>No; defaults to all (<q>**</q>)</td>
  86. </tr>
  87. <tr>
  88. <td>includesfile</td>
  89. <td>name of a file. Each line of this file is taken to be an include pattern</td>
  90. <td>No</td>
  91. </tr>
  92. <tr>
  93. <td>excludes</td>
  94. <td>comma- or space-separated list of patterns of files that must be excluded.</td>
  95. <td>No; defaults to default excludes or none if <var>defaultexcludes</var> is <q>no</q></td>
  96. </tr>
  97. <tr>
  98. <td>excludesfile</td>
  99. <td>name of a file. Each line of this file is taken to be an exclude pattern</td>
  100. <td>No</td>
  101. </tr>
  102. <tr>
  103. <td>defaultexcludes</td>
  104. <td>indicates whether default excludes should be used or not (<q>yes|no</q>).</td>
  105. <td>No; defaults to <q>yes</q></td>
  106. </tr>
  107. <tr>
  108. <td>preserveLastModified</td>
  109. <td>Keep the file timestamp(s) even if the file(s) is(are) modified. <em>since Apache Ant
  110. 1.8.0</em>.</td>
  111. <td>No; defaults to <q>false</q></td>
  112. </tr>
  113. <tr>
  114. <td>failOnNoReplacements</td>
  115. <td>Whether to fail the build if the task didn't do anything. <em>since Ant 1.8.0</em>.</td>
  116. <td>No; defaults to <q>false</q></td>
  117. </tr>
  118. </table>
  119. <h3>Examples</h3>
  120. <pre>&lt;replace file=&quot;${src}/index.html&quot; token=&quot;@@@&quot; value=&quot;wombat&quot;/&gt;</pre>
  121. <p>replaces occurrences of the string <q>@@@</q> with the string <q>wombat</q>, in the
  122. file <samp>${src}/index.html</samp>.</p>
  123. <h3>Parameters specified as nested elements</h3>
  124. <p>This task forms an implicit <a href="../Types/fileset.html">FileSet</a> and supports most
  125. attributes of <code>&lt;fileset&gt;</code> as well as the
  126. nested <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code>
  127. and <code>&lt;patternset&gt;</code> elements.</p>
  128. <p><em>Since Ant 1.8.0</em>, this task supports any filesystem
  129. based <a href="../Types/resources.html#collection">resource collections</a> as nested elements.</p>
  130. <h4>replacetoken and replacevalue</h4>
  131. <p>If either the text you want to replace or the replacement text cross line boundaries, you can use
  132. nested elements to specify them.</p>
  133. <p>The elements support attributes:</p>
  134. <table class="attr">
  135. <tr>
  136. <th scope="col">Attribute</th>
  137. <th scope="col">Description</th>
  138. <th scope="col">Required</th>
  139. </tr>
  140. <tr>
  141. <td>expandProperties</td>
  142. <td>Whether to expand properties in the nested text. <em>Since Ant 1.8.0</em>.</td>
  143. <td>No; defaults to <q>true</q></td>
  144. </tr>
  145. </table>
  146. <h3>Examples</h3>
  147. <pre>
  148. &lt;replace dir=&quot;${src}&quot; value=&quot;wombat&quot;&gt;
  149. &lt;include name=&quot;**/*.html&quot;/&gt;
  150. &lt;replacetoken&gt;&lt;![CDATA[multi line
  151. token]]&gt;&lt;/replacetoken&gt;
  152. &lt;/replace&gt;</pre>
  153. <p>replaces occurrences of the string <q>multi line<i>\n</i>token</q> with the string <q>wombat</q>,
  154. in all HTML files in the directory <samp>${src}</samp>. Here, <q><i>\n</i></q> is the platform
  155. specific line separator.</p>
  156. <pre>
  157. &lt;replace file=&quot;${src}/index.html&quot;&gt;
  158. &lt;replacetoken&gt;&lt;![CDATA[two line
  159. token]]&gt;&lt;/replacetoken&gt;
  160. &lt;replacevalue&gt;&lt;![CDATA[two line
  161. token]]&gt;&lt;/replacevalue&gt;
  162. &lt;/replace&gt;</pre>
  163. <h4>replacefilter</h4>
  164. <p>In addition to allowing for multiple replacements, optional
  165. nested <code>&lt;replacefilter&gt;</code> elements allow replacement values to be extracted from a
  166. property file. The name of this file is specified using the <code>&lt;replace&gt;</code>
  167. attribute <var>propertyFile</var>.</p>
  168. <table class="attr">
  169. <tr>
  170. <th scope="col">Attribute</th>
  171. <th scope="col">Description</th>
  172. <th scope="col">Required</th>
  173. </tr>
  174. <tr>
  175. <td>token</td>
  176. <td>The string to search for.</td>
  177. <td>Yes, unless a nested <code>replacetoken</code>
  178. is specified</td>
  179. </tr>
  180. <tr>
  181. <td>value</td>
  182. <td>The replacement string.</td>
  183. <td rowspan="2">Either may be specified, but not both. Both can be omitted, if desired.</td>
  184. </tr>
  185. <tr>
  186. <td>property</td>
  187. <td class="left">Name of the property whose value is to serve as the replacement value.</td>
  188. </tr>
  189. </table>
  190. <p><em>Since Ant 1.8.0</em>, <var>token</var> and <var>value</var> can be specified as nested
  191. elements just like in the task itself.</p>
  192. <p>If neither <var>value</var> nor <var>property</var> is used, the value provided using
  193. the <code>&lt;replace&gt;</code> attribute <var>value</var> and/or
  194. the <code>&lt;replacevalue&gt;</code> element is used. If no value was specified using either of
  195. these options, the token is replaced with an empty string.</p>
  196. <h3>Examples</h3>
  197. <p>In file <samp>configure.sh</samp>, replace all instances of <q>@token1@</q>
  198. with <q>defaultvalue</q>, all instances of <q>@token2@</q> with <q>value2</q>, and all instances
  199. of <q>@token3@</q> with the value of the property <code>property.key</code>, as it appears in
  200. property file <samp>src/name.properties</samp>.</p>
  201. <pre>
  202. &lt;replace file=&quot;configure.sh&quot;
  203. value=&quot;defaultvalue&quot;
  204. propertyFile=&quot;src/name.properties&quot;&gt;
  205. &lt;replacefilter token=&quot;@token1@&quot;/&gt;
  206. &lt;replacefilter token=&quot;@token2@&quot;
  207. value=&quot;value2&quot;/&gt;
  208. &lt;replacefilter token=&quot;@token3@&quot;
  209. property=&quot;property.key&quot;/&gt;
  210. &lt;replacefilter&gt;
  211. &lt;replacetoken&gt;@token4@&lt;/replacetoken&gt;
  212. &lt;replacevalue&gt;value4&lt;/replacevalue&gt;
  213. &lt;/replacefilter&gt;
  214. &lt;/replace&gt;</pre>
  215. <p><strong>Note</strong>: It is possible to use either
  216. the <var>token</var>/<code>&lt;replacetoken&gt;</code>
  217. and <var>value</var>/<code>&lt;replacevalue&gt;</code> attributes/elements, the
  218. nested <code>replacefilter</code> elements, or both in the same operation.</p>
  219. </body>
  220. </html>