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.

property.html 8.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Property Task</title>
  5. </head>
  6. <body>
  7. <h2><a name="property">Property</a></h2>
  8. <h3>Description</h3>
  9. <p>Sets a property (by name and value), or set of properties (from file or
  10. resource) in the project. Properties are case sensitive.</p>
  11. Properties are immutable: whoever sets a property first freezes it for the
  12. rest of the build; they are most definately not variable.
  13. <p>There are six ways to set properties:</p>
  14. <ul>
  15. <li>By supplying both the <i>name</i> and <i>value</i> attribute.</li>
  16. <li>By supplying both the <i>name</i> and <i>refid</i> attribute.</li>
  17. <li>By setting the <i>file</i> attribute with the filename of the property
  18. file to load. This property file has the format as defined by the file used
  19. in the class java.util.Properties, with the same rules about how
  20. non-ISO8859-1 characters must be escaped.</li>
  21. <li>By setting the <i>url</i> attribute with the url from which to load the
  22. properties. This url must be directed to a file that has the format as defined
  23. by the file used in the class java.util.Properties.</li>
  24. <li>By setting the <i>resource</i> attribute with the resource name of the
  25. property file to load. A resource is a property file on the current
  26. classpath, or on the specified classpath.</li>
  27. <li>By setting the <i>environment</i> attribute with a prefix to use.
  28. Properties will be defined for every environment variable by
  29. prefixing the supplied name and a period to the name of the variable.</li>
  30. </ul>
  31. <p>Although combinations of these ways are possible, only one should be used
  32. at a time. Problems might occur with the order in which properties are set, for
  33. instance.</p>
  34. <p>The value part of the properties being set, might contain references to other
  35. properties. These references are resolved at the time these properties are set.
  36. This also holds for properties loaded from a property file.</p>
  37. <p>A list of predefined properties can be found <a
  38. href="../using.html#built-in-props">here</a>.</p>
  39. <h4>OpenVMS Users</h4>
  40. <p>With the <code>environment</code> attribute this task will load all defined
  41. logicals on an OpenVMS system. Logicals with multiple equivalence names get
  42. mapped to a property whose value is a comma separated list of all equivalence
  43. names. If a logical is defined in multiple tables, only the most local
  44. definition is available (the table priority order being PROCESS, JOB, GROUP,
  45. SYSTEM).
  46. </p>
  47. <h3>Parameters</h3>
  48. <table border="1" cellpadding="2" cellspacing="0">
  49. <tr>
  50. <td valign="top"><b>Attribute</b></td>
  51. <td valign="top"><b>Description</b></td>
  52. <td align="center" valign="top"><b>Required</b></td>
  53. </tr>
  54. <tr>
  55. <td valign="top">name</td>
  56. <td valign="top">the name of the property to set.</td>
  57. <td valign="top" align="center">No</td>
  58. </tr>
  59. <tr>
  60. <td valign="top">value</td>
  61. <td valign="top">the value of the property.</td>
  62. <td valign="middle" align="center" rowspan="3">One of these, when using the
  63. name attribute</td>
  64. </tr>
  65. <tr>
  66. <td valign="top">location</td>
  67. <td valign="top">Sets the property to the absolute filename of the
  68. given file. If the value of this attribute is an absolute path, it
  69. is left unchanged (with / and \ characters converted to the
  70. current platforms conventions). Otherwise it is taken as a path
  71. relative to the project's basedir and expanded.</td>
  72. </tr>
  73. <tr>
  74. <td valign="top">refid</td>
  75. <td valign="top"><a href="../using.html#references">Reference</a> to an object
  76. defined elsewhere. Only yields reasonable results for references
  77. to <a href="../using.html#path">PATH like structures</a> or properties.</td>
  78. </tr>
  79. <tr>
  80. <td valign="top">resource</td>
  81. <td valign="top">the resource name of the property file.</td>
  82. <td valign="middle" align="center" rowspan="4">One of these, when
  83. <b>not</b> using the name attribute</td>
  84. </tr>
  85. <tr>
  86. <td valign="top">file</td>
  87. <td valign="top">the filename of the property file .</td>
  88. </tr>
  89. <tr>
  90. <td valign="top">url</td>
  91. <td valign="top">the url from which to read properties.</td>
  92. </tr>
  93. <tr>
  94. <td valign="top">environment</td>
  95. <td valign="top">the prefix to use when retrieving environment variables. Thus
  96. if you specify environment=&quot;myenv&quot; you will be able to access OS-specific
  97. environment variables via property names &quot;myenv.PATH&quot; or
  98. &quot;myenv.TERM&quot;. Note that if you supply a property name with a final
  99. &quot;.&quot; it will not be doubled. ie environment=&quot;myenv.&quot; will still
  100. allow access of environment variables through &quot;myenv.PATH&quot; and
  101. &quot;myenv.TERM&quot;. This functionality is currently only implemented
  102. on select platforms. Feel free to send patches to increase the number of platforms
  103. this functionality is supported on ;).<br>
  104. Note also that properties are case sensitive, even if the
  105. environment variables on your operating system are not, e.g. it
  106. will be ${env.Path} not ${env.PATH} on Windows 2000.</td>
  107. </tr>
  108. <tr>
  109. <td valign="top">classpath</td>
  110. <td valign="top">the classpath to use when looking up a resource.</td>
  111. <td align="center" valign="top">No</td>
  112. </tr>
  113. <tr>
  114. <td valign="top">classpathref</td>
  115. <td valign="top">the classpath to use when looking up a resource,
  116. given as <a href="../using.html#references">reference</a> to a <code>&lt;path&gt;</code> defined
  117. elsewhere..</td>
  118. <td align="center" valign="top">No</td>
  119. </tr>
  120. <tr>
  121. <td valign="top">prefix</td>
  122. <td valign="top">Prefix to apply to properties loaded using <code>file</code>
  123. or <code>resource</code>. A "." is appended to the prefix if not specified.</td>
  124. <td align="center" valign="top">No</td>
  125. </tr>
  126. </table>
  127. <h3>Parameters specified as nested elements</h3>
  128. <h4>classpath</h4>
  129. <p><code>Property</code>'s <i>classpath</i> attribute is a <a
  130. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  131. <i>classpath</i> element.</p>
  132. <h3>Examples</h3>
  133. <pre> &lt;property name=&quot;foo.dist&quot; value=&quot;dist&quot;/&gt;</pre>
  134. <p>sets the property <code>foo.dist</code> to the value &quot;dist&quot;.</p>
  135. <pre> &lt;property file=&quot;foo.properties&quot;/&gt;</pre>
  136. <p>reads a set of properties from a file called &quot;foo.properties&quot;.</p>
  137. <pre> &lt;property url=&quot;http://www.mysite.com/bla/props/foo.properties&quot;/&gt;</pre>
  138. <p>reads a set of properties from the address &quot;http://www.mysite.com/bla/props/foo.properties&quot;.</p>
  139. <pre> &lt;property resource=&quot;foo.properties&quot;/&gt;</pre>
  140. <p>reads a set of properties from a resource called &quot;foo.properties&quot;.</p>
  141. <p>Note that you can reference a global properties file for all of your Ant
  142. builds using the following:</p>
  143. <pre> &lt;property file=&quot;${user.home}/.ant-global.properties&quot;/&gt;</pre>
  144. <p>since the &quot;user.home&quot; property is defined by the Java virtual machine
  145. to be your home directory. Where the &quot;user.home&quot; property resolves to in
  146. the file system depends on the operating system version and the JVM implementation.
  147. On Unix based systems, this will map to the user's home directory. On modern Windows
  148. variants, this will most likely resolve to the user's directory in the &quot;Documents
  149. and Settings&quot; folder. Older windows variants such as Windows 98/ME are less
  150. predictable, as are other operating system/JVM combinations.</p>
  151. <pre>
  152. &lt;property environment=&quot;env&quot;/&gt;
  153. &lt;echo message=&quot;Number of Processors = ${env.NUMBER_OF_PROCESSORS}&quot;/&gt;
  154. &lt;echo message=&quot;ANT_HOME is set to = ${env.ANT_HOME}&quot;/&gt;
  155. </pre>
  156. <p>reads the system environment variables and stores them in properties, prefixed with &quot;env&quot;.
  157. Note that this only works on <em>select</em> operating systems.
  158. Two of the values are shown being echoed.
  159. </p>
  160. <h3>Property Files</h3>
  161. As stated, this task will load in a properties file stored in the file
  162. system, or as a resource on a classpath. Here are some interesting facts
  163. about this feature
  164. <ol>
  165. <li>If the file is not there, nothing is printed except at -verbose log
  166. level. This lets you have optional configuration files for every
  167. project, that team members can customize.
  168. <li>The rules for this format are laid down
  169. <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream)">by Sun</a>.
  170. This makes it hard for Team Ant to field bug reports about it.
  171. <li>Trailing spaces are not stripped. It may have been what you wanted.
  172. <li>Want unusual characters? Escape them \u0456 or \" style.
  173. <li>Ant Properties are expanded in the file.
  174. </ol>
  175. In-file property expansion is very cool. Learn to use it.
  176. <p>
  177. Example:
  178. <pre>
  179. build.compiler=jikes
  180. deploy.server=lucky
  181. deploy.port=8080
  182. deploy.url=http://${deploy.server}:${deploy.port}/
  183. </pre>
  184. <hr>
  185. <p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
  186. Reserved.</p>
  187. </body>
  188. </html>