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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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>Property Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="property">Property</a></h2>
  23. <h3>Description</h3>
  24. <p>Sets a <a href="../using.html#properties">property</a>
  25. (by name and value), or set of properties (from file or
  26. resource) in the project. Properties are case sensitive.</p>
  27. Properties are immutable: whoever sets a property first freezes it for the
  28. rest of the build; they are most definitely not variables.
  29. <p>There are seven ways to set properties:</p>
  30. <ul>
  31. <li>By supplying both the <i>name</i> and one of <i>value</i> or <i>location</i> attribute.</li>
  32. <li>By supplying the <i>name</i> and nested text.</li>
  33. <li>By supplying both the <i>name</i> and <i>refid</i> attribute.</li>
  34. <li>By setting the <i>file</i> attribute with the filename of the property
  35. file to load. This property file has the format as defined by the file used
  36. in the class java.util.Properties, with the same rules about how
  37. non-ISO8859-1 characters must be escaped.</li>
  38. <li>By setting the <i>url</i> attribute with the url from which to load the
  39. properties. This url must be directed to a file that has the format as defined
  40. by the file used in the class java.util.Properties.</li>
  41. <li>By setting the <i>resource</i> attribute with the resource name of the
  42. property file to load. A resource is a property file on the current
  43. classpath, or on the specified classpath.</li>
  44. <li>By setting the <i>environment</i> attribute with a prefix to use.
  45. Properties will be defined for every environment variable by
  46. prefixing the supplied name and a period to the name of the variable.</li>
  47. </ul>
  48. <p>Although combinations of these ways are possible, only one should be used
  49. at a time. Problems might occur with the order in which properties are set, for
  50. instance.</p>
  51. <p>The value part of the properties being set, might contain references to other
  52. properties. These references are resolved at the time these properties are set.
  53. This also holds for properties loaded from a property file.</p>
  54. <p>A list of predefined properties can be found <a
  55. href="../properties.html#built-in-props">here</a>.</p>
  56. <p>Since Ant 1.7.1 it is possible to load properties defined in xml
  57. according to <a href="http://java.sun.com/dtd/properties.dtd">Suns DTD</a>,
  58. if Java5+ is present. For this the name of the file, resource or url has
  59. to end with <tt>.xml</tt>.</p>
  60. <h4>OpenVMS Users</h4>
  61. <p>With the <code>environment</code> attribute this task will load all defined
  62. logicals on an OpenVMS system. Logicals with multiple equivalence names get
  63. mapped to a property whose value is a comma separated list of all equivalence
  64. names. If a logical is defined in multiple tables, only the most local
  65. definition is available (the table priority order being PROCESS, JOB, GROUP,
  66. SYSTEM).
  67. </p>
  68. <h3>Parameters</h3>
  69. <table border="1" cellpadding="2" cellspacing="0">
  70. <tr>
  71. <td valign="top"><b>Attribute</b></td>
  72. <td valign="top"><b>Description</b></td>
  73. <td align="center" valign="top"><b>Required</b></td>
  74. </tr>
  75. <tr>
  76. <td valign="top">name</td>
  77. <td valign="top">the name of the property to set.</td>
  78. <td valign="top" align="center">No</td>
  79. </tr>
  80. <tr>
  81. <td valign="top">value</td>
  82. <td valign="top">the value of the property.</td>
  83. <td valign="middle" align="center" rowspan="3">One of these or
  84. nested text, when using the name attribute</td>
  85. </tr>
  86. <tr>
  87. <td valign="top">location</td>
  88. <td valign="top">Sets the property to the absolute filename of the
  89. given file. If the value of this attribute is an absolute path, it
  90. is left unchanged (with / and \ characters converted to the
  91. current platforms conventions). Otherwise it is taken as a path
  92. relative to the project's basedir and expanded.</td>
  93. </tr>
  94. <tr>
  95. <td valign="top">refid</td>
  96. <td valign="top"><a href="../using.html#references">Reference</a> to an object
  97. defined elsewhere. Only yields reasonable results for references
  98. to <a href="../using.html#path">PATH like structures</a> or properties.</td>
  99. </tr>
  100. <tr>
  101. <td valign="top">resource</td>
  102. <td valign="top"> the name of the classpath resource containing
  103. properties settings in properties file format.</td>
  104. <td valign="middle" align="center" rowspan="4">One of these, when
  105. <b>not</b> using the name attribute</td>
  106. </tr>
  107. <tr>
  108. <td valign="top">file</td>
  109. <td valign="top">the location of the properties file to load.</td>
  110. </tr>
  111. <tr>
  112. <td valign="top">url</td>
  113. <td valign="top">a url containing properties-format settings.</td>
  114. </tr>
  115. <tr>
  116. <td valign="top">environment</td>
  117. <td valign="top">the prefix to use when retrieving environment variables. Thus
  118. if you specify environment=&quot;myenv&quot; you will be able to access OS-specific
  119. environment variables via property names &quot;myenv.PATH&quot; or
  120. &quot;myenv.TERM&quot;. Note that if you supply a property name with a final
  121. &quot;.&quot; it will not be doubled; i.e. environment=&quot;myenv.&quot; will still
  122. allow access of environment variables through &quot;myenv.PATH&quot; and
  123. &quot;myenv.TERM&quot;. This functionality is currently only implemented
  124. on <a href="#notes-env">select platforms</a>. Feel free to send patches to increase the
  125. number of platforms on which this functionality is supported ;).<br>
  126. Note also that properties are case-sensitive, even if the
  127. environment variables on your operating system are not; e.g. Windows 2000's
  128. system path variable is set to an Ant property named "env.Path"
  129. rather than "env.PATH".</td>
  130. </tr>
  131. <tr>
  132. <td valign="top">classpath</td>
  133. <td valign="top">the classpath to use when looking up a resource.</td>
  134. <td align="center" valign="top">No</td>
  135. </tr>
  136. <tr>
  137. <td valign="top">classpathref</td>
  138. <td valign="top">the classpath to use when looking up a resource,
  139. given as <a href="../using.html#references">reference</a> to a <code>&lt;path&gt;</code> defined
  140. elsewhere..</td>
  141. <td align="center" valign="top">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">prefix</td>
  145. <td valign="top">Prefix to apply to properties loaded using <code>file</code>,
  146. <code>resource</code>, or <code>url</code>.
  147. A "." is appended to the prefix if not specified.</td>
  148. <td align="center" valign="top">No</td>
  149. </tr>
  150. <tr>
  151. <td valign="top">relative</td>
  152. <td valign="top">If set to <tt>true</tt> the relative path
  153. to <tt>basedir</tt> is set. <em>Since Ant 1.8.0</em></td>
  154. <td align="center" valign="top">No (default=<tt>false</tt>)</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">basedir</td>
  158. <td valign="top">The basedir to calculate the relative path
  159. from. <em>Since Ant 1.8.0</em></td>
  160. <td align="center" valign="top">No (default=<tt>${basedir}</tt>)</td>
  161. </tr>
  162. </table>
  163. <h3>Parameters specified as nested elements</h3>
  164. <h4>classpath</h4>
  165. <p><code>Property</code>'s <i>classpath</i> attribute is a <a
  166. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  167. <i>classpath</i> element.</p>
  168. <h3>Examples</h3>
  169. <pre> &lt;property name=&quot;foo.dist&quot; value=&quot;dist&quot;/&gt;</pre>
  170. <p>sets the property <code>foo.dist</code> to the value &quot;dist&quot;.</p>
  171. <pre> &lt;property name=&quot;foo.dist&quot;&gt;dist&lt;/property&gt;</pre>
  172. <p>sets the property <code>foo.dist</code> to the value &quot;dist&quot;.</p>
  173. <pre> &lt;property file=&quot;foo.properties&quot;/&gt;</pre>
  174. <p>reads a set of properties from a file called &quot;foo.properties&quot;.</p>
  175. <pre> &lt;property url=&quot;http://www.mysite.com/bla/props/foo.properties&quot;/&gt;</pre>
  176. <p>reads a set of properties from the address &quot;http://www.mysite.com/bla/props/foo.properties&quot;.</p>
  177. <pre> &lt;property resource=&quot;foo.properties&quot;/&gt;</pre>
  178. <p>reads a set of properties from a resource called &quot;foo.properties&quot;.</p>
  179. <p>Note that you can reference a global properties file for all of your Ant
  180. builds using the following:</p>
  181. <pre> &lt;property file=&quot;${user.home}/.ant-global.properties&quot;/&gt;</pre>
  182. <p>since the &quot;user.home&quot; property is defined by the Java virtual machine
  183. to be your home directory. Where the &quot;user.home&quot; property resolves to in
  184. the file system depends on the operating system version and the JVM implementation.
  185. On Unix based systems, this will map to the user's home directory. On modern Windows
  186. variants, this will most likely resolve to the user's directory in the &quot;Documents
  187. and Settings&quot; folder. Older windows variants such as Windows 98/ME are less
  188. predictable, as are other operating system/JVM combinations.</p>
  189. <pre>
  190. &lt;property environment=&quot;env&quot;/&gt;
  191. &lt;echo message=&quot;Number of Processors = ${env.NUMBER_OF_PROCESSORS}&quot;/&gt;
  192. &lt;echo message=&quot;ANT_HOME is set to = ${env.ANT_HOME}&quot;/&gt;
  193. </pre>
  194. <p>reads the system environment variables and stores them in properties, prefixed with &quot;env&quot;.
  195. Note that this only works on <em>select</em> operating systems.
  196. Two of the values are shown being echoed.
  197. </p>
  198. <pre>
  199. &lt;property environment=&quot;env&quot;/&gt;
  200. &lt;property file=&quot;${user.name}.properties&quot;/&gt;
  201. &lt;property file=&quot;${env.STAGE}.properties&quot;/&gt;
  202. &lt;property file=&quot;build.properties&quot;/&gt;
  203. </pre>
  204. <p>This buildfile uses the properties defined in <tt>build.properties</tt>. Regarding to the
  205. environment variable <tt>STAGE</tt> some or all values could be overwritten, e.g. having
  206. <tt>STAGE=test</tt> and a <tt>test.properties</tt> you have special values for that (like another
  207. name for the test server). Finally all these values could be overwritten by personal settings with
  208. a file per user.</p>
  209. <pre>
  210. &lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;..&quot;/&gt;
  211. </pre>
  212. <p>Stores the relative path in <tt>foo</tt>: projectbasedir/my/file.txt</p>
  213. <pre>
  214. &lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;cvs&quot;/&gt;
  215. </pre>
  216. <p>Stores the relative path in <tt>foo</tt>: ../my/file.txt</p>
  217. <h3>Property Files</h3>
  218. As stated, this task will load in a properties file stored in the file
  219. system, or as a resource on a classpath. Here are some interesting facts
  220. about this feature
  221. <ol>
  222. <li>If the file is not there, nothing is printed except at -verbose log
  223. level. This lets you have optional configuration files for every
  224. project, that team members can customize.
  225. <li>The rules for this format are laid down
  226. <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html#load(java.io.InputStream)">by Sun</a>.
  227. This makes it hard for Team Ant to field bug reports about it.
  228. <li>Trailing spaces are not stripped. It may have been what you wanted.
  229. <li>Want unusual characters? Escape them \u0456 or \" style.
  230. <li>Ant Properties are expanded in the file.
  231. </ol>
  232. In-file property expansion is very cool. Learn to use it.
  233. <p>
  234. Example:
  235. <pre>
  236. build.compiler=jikes
  237. deploy.server=lucky
  238. deploy.port=8080
  239. deploy.url=http://${deploy.server}:${deploy.port}/
  240. </pre>
  241. <a name="notes-env"></a>
  242. <h3>Notes about environment variables</h3>
  243. <p>
  244. Ant runs on Java 1.2 therefore it cant use Java5 features for accessing environment
  245. variables. So it starts a command in a new process which prints the environment variables,
  246. analyzes the output and creates the properties. <br>
  247. There are commands for the following operating systems implemented in
  248. <a href="http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Execute.java?view=markup">
  249. Execute.java</a> (method <tt>getProcEnvCommand()</tt>):
  250. <table>
  251. <tr>
  252. <th>OS</th>
  253. <th>command</th>
  254. </tr>
  255. <tr>
  256. <td> os/2 </td>
  257. <td> cmd /c set </td>
  258. </tr>
  259. <tr>
  260. <td colspan="2"> windows </td>
  261. </tr>
  262. <tr>
  263. <td> * win9x </td>
  264. <td> command.com /c set </td>
  265. </tr>
  266. <tr>
  267. <td> * other </td>
  268. <td> cmd /c set </td>
  269. </tr>
  270. <tr>
  271. <td> z/os </td>
  272. <td> /bin/env <b>OR</b> /usr/bin/env <b>OR</b> env <i>(depending on read rights)</i> </td>
  273. </tr>
  274. <tr>
  275. <td> unix </td>
  276. <td> /bin/env <b>OR</b> /usr/bin/env <b>OR</b> env <i>(depending on read rights)</i> </td>
  277. </tr>
  278. <tr>
  279. <td> netware </td>
  280. <td> env </td>
  281. </tr>
  282. <tr>
  283. <td> os/400 </td>
  284. <td> env </td>
  285. </tr>
  286. <tr>
  287. <td> openvms </td>
  288. <td> show logical </td>
  289. </tr>
  290. </table>
  291. </p>
  292. </body>
  293. </html>