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 10 KiB

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