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.

propertyfile.html 8.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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>PropertyFile Task</title>
  20. </head>
  21. <body>
  22. <h1>PropertyFile</h1>
  23. <hr/>
  24. <h2 id="introduction">Introduction</h2>
  25. <p>Apache Ant provides an optional task for editing property files. This is very useful when wanting
  26. to make unattended modifications to configuration files for application servers and
  27. applications. Currently, the task maintains a working property file with the ability to add
  28. properties or make changes to existing ones. <em>Since Ant 1.8.0</em> comments and layout of the
  29. original properties file are preserved.</p>
  30. <p><em>Since Ant 1.8.2</em> the linefeed-style of the original file will be preserved as well, as
  31. long as style used to be consistent. In general, linefeeds of the updated file will be the same as
  32. the first linefeed found when reading it.</p>
  33. <hr/>
  34. <h2 id="proptask">PropertyFile Task</h2>
  35. <h3>Parameters</h3>
  36. <table class="attr">
  37. <tr>
  38. <th scope="col">Attribute</th>
  39. <th scope="col">Description</th>
  40. <th scope="col">Required</th>
  41. </tr>
  42. <tr>
  43. <td>file</td>
  44. <td>Location of the property file to be edited</td>
  45. <td>Yes</td>
  46. </tr>
  47. <tr>
  48. <td>comment</td>
  49. <td>Header for the file itself</td>
  50. <td>No</td>
  51. </tr>
  52. <tr>
  53. <td>jdkproperties</td>
  54. <td>Use <code>java.lang.Properties</code>, which will lose comments and layout of file. <em>since
  55. Ant 1.8.0</em></td>
  56. <td>No; default is <q>false</q></td>
  57. </tr>
  58. </table>
  59. <p>The boolean attribute <var>jdkproperties</var> is provided to recover the previous behaviour of
  60. the task, in which the layout and any comments in the properties file were lost by the task.</p>
  61. <h3>Parameters specified as nested elements</h3>
  62. <h4 id="entryElement">Entry</h4>
  63. <p>Use nested <code>&lt;entry&gt;</code> elements to specify actual modifications to the property
  64. file itself.</p>
  65. <table class="attr">
  66. <tr>
  67. <th scope="col">Attribute</th>
  68. <th scope="col">Description</th>
  69. <th scope="col">Required</th>
  70. </tr>
  71. <tr>
  72. <td>key</td>
  73. <td>Name of the property name/value pair</td>
  74. <td>Yes</td>
  75. </tr>
  76. <tr>
  77. <td>value</td>
  78. <td>Value to set (<q>=</q>), to add (<q>+</q>) or subtract (<q>-</q>)</td>
  79. <td rowspan="2">At least one must be specified, if <var>operation</var> is not <q>del</q></td>
  80. </tr>
  81. <tr>
  82. <td>default</td>
  83. <td class="left">Initial value to set for a property if it is not already defined in the
  84. property file.<br/>For type <var>date</var>, an additional keyword is
  85. allowed: <q>now</q></td></tr>
  86. <tr>
  87. <td>type</td>
  88. <td>Regard the value as: <q>int</q>, <q>date</q> or <q>string</q> (default)</td>
  89. <td>No; defaults to <q>string</q></td>
  90. </tr>
  91. <tr>
  92. <td>operation</td>
  93. <td>One of the following operations:<br/>
  94. <strong>for all datatypes:</strong>
  95. <ul>
  96. <li><q>del</q> : deletes an entry</li>
  97. <li><q>+</q> : adds a value to the existing value</li>
  98. <li><q>=</q> : sets a value instead of the existing value (default)</li>
  99. </ul>
  100. <strong>for <var>date</var> and <var>int</var> only:</strong>
  101. <ul>
  102. <li><q>-</q> : subtracts a value from the existing value</li>
  103. </ul>
  104. </td>
  105. <td>No; defaults to <q>=</q></td>
  106. </tr>
  107. <tr>
  108. <td>pattern</td>
  109. <td>For <var>int</var> and <var>date</var> type only. If present, values will be parsed and
  110. formatted accordingly.</td>
  111. <td>No</td>
  112. </tr>
  113. <tr>
  114. <td>unit</td>
  115. <td>The unit of the value to be applied to <var>date</var> <q>+</q>/<q>-</q> operations. Valid
  116. Values are:
  117. <ul>
  118. <li><q>millisecond</q></li>
  119. <li><q>second</q></li>
  120. <li><q>minute</q></li>
  121. <li><q>hour</q></li>
  122. <li><q>day</q> (default)</li>
  123. <li><q>week</q></li>
  124. <li><q>month</q></li>
  125. <li><q>year</q></li>
  126. </ul>
  127. This only applies to <var>date</var> types using a <q>+</q>/<q>-</q> operation.
  128. </td>
  129. <td>No; defaults to <q>day</q></td>
  130. </tr>
  131. </table>
  132. <p>The rules used when setting a property value are shown below. The operation
  133. occurs <strong>after</strong> these rules are considered.</p>
  134. <ul>
  135. <li>If only <var>value</var> is specified, the property is set to it regardless of its previous
  136. value.</li>
  137. <li>If only <var>default</var> is specified and the property previously existed in the property
  138. file, it is unchanged.</li>
  139. <li>If only <var>default</var> is specified and the property did not exist in the property file,
  140. the property is set to <var>default</var>.</li>
  141. <li>If <var>value</var> and <var>default</var> are both specified and the property previously
  142. existed in the property file, the property is set to <var>value</var>.</li>
  143. <li>If <var>value</var> and <var>default</var> are both specified and the property did not exist
  144. in the property file, the property is set to <var>default</var>.</li>
  145. </ul>
  146. <h3>Examples</h3>
  147. <p>The following changes the <samp>my.properties</samp> file. Assume <samp>my.properties</samp>
  148. looks like:</p>
  149. <pre># A string value
  150. akey=original value
  151. # The following is a counter, which will be incremented by 1 for
  152. # each time the build is run.
  153. anint=1</pre>
  154. <p>After running, the file would now look like</p>
  155. <pre>#My properties
  156. #Wed Aug 31 13:47:19 BST 2005
  157. # A string value
  158. akey=avalue
  159. # The following is a counter, which will be incremented by 1 for
  160. # each time the build is run.
  161. anint=2
  162. adate=2005/08/31 13\:47
  163. formated.int=0014
  164. formated.date=243 13\:47</pre>
  165. <p>The slashes conform to the expectations of the Properties class. The file will be stored in a
  166. manner so that each character is examined and escaped if necessary.</p>
  167. <p>The layout and comment of the original file is preserved. New properties are added at the end of
  168. the file. Existing properties are overwritten in place.</p>
  169. <pre>
  170. &lt;propertyfile file=&quot;my.properties&quot;
  171. comment=&quot;My properties&quot;&gt;
  172. &lt;entry key=&quot;akey&quot; value=&quot;avalue&quot;/&gt;
  173. &lt;entry key=&quot;adate&quot; type=&quot;date&quot; value=&quot;now&quot;/&gt;
  174. &lt;entry key=&quot;anint&quot; type=&quot;int&quot; default=&quot;0&quot; operation=&quot;+&quot;/&gt;
  175. &lt;entry key=&quot;formated.int&quot; type=&quot;int&quot; default=&quot;0013&quot; operation=&quot;+&quot; pattern=&quot;0000&quot;/&gt;
  176. &lt;entry key=&quot;formated.date&quot; type=&quot;date&quot; value=&quot;now&quot; pattern=&quot;DDD HH:mm&quot;/&gt;
  177. &lt;/propertyfile&gt;</pre>
  178. <p>To produce dates relative from today:</p>
  179. <pre>
  180. &lt;propertyfile file=&quot;my.properties&quot;
  181. comment=&quot;My properties&quot;&gt;
  182. &lt;entry key=&quot;formated.date-1&quot;
  183. type=&quot;date&quot; default=&quot;now&quot; pattern=&quot;DDD&quot;
  184. operation=&quot;-&quot; value=&quot;1&quot;/&gt;
  185. &lt;entry key=&quot;formated.tomorrow&quot;
  186. type=&quot;date&quot; default=&quot;now&quot; pattern=&quot;DDD&quot;
  187. operation=&quot;+&quot; value=&quot;1&quot;/&gt;
  188. &lt;/propertyfile&gt;</pre>
  189. <p>Concatenation of strings:</p>
  190. <pre>
  191. &lt;propertyfile file=&quot;my.properties&quot;
  192. comment=&quot;My properties&quot;&gt;
  193. &lt;entrykey=&quot;progress&quot; default=&quot;&quot; operation=&quot;+&quot; value=&quot;.&quot;/&gt;
  194. &lt;/propertyfile&gt;</pre>
  195. <p>Each time called, a <q>.</q> will be appended to <code>progress</code></p>
  196. <p>Pumps the project version to the next minor version (increase minor and
  197. set <code>patch=0</code>):</p>
  198. <pre>
  199. &lt;target name="nextMinorVersion"&gt;
  200. &lt;property name="header"
  201. value="##Generated file - do not modify!"/&gt;
  202. &lt;propertyfile file="version.properties" comment="${header}"&gt;
  203. &lt;entry key="product.build.major" type="int" value="3"/&gt;
  204. &lt;entry key="product.build.minor" type="int" operation="+"/&gt;
  205. &lt;entry key="product.build.patch" type="int" value="0"/&gt;
  206. &lt;entry key="product.build.date" type="date" value="now"/&gt;
  207. &lt;/propertyfile&gt;
  208. &lt;/target&gt;</pre>
  209. <p>After running this target the version changed e.g. from 3.2.2 to 3.3.0.</p>
  210. </body>
  211. </html>