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.6 KiB

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