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

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 id="property">Property</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><em>Since Apache Ant 1.8.0</em>, 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 Java 5+ is present. For this the name of the file, resource or url has
  59. to end with <tt>.xml</tt>.</p>
  60. <h3>Parameters</h3>
  61. <table>
  62. <tr>
  63. <td valign="top"><b>Attribute</b></td>
  64. <td valign="top"><b>Description</b></td>
  65. <td align="center" valign="top"><b>Required</b></td>
  66. </tr>
  67. <tr>
  68. <td valign="top">name</td>
  69. <td valign="top">the name of the property to set.</td>
  70. <td valign="top" align="center">No</td>
  71. </tr>
  72. <tr>
  73. <td valign="top">value</td>
  74. <td valign="top">the value of the property.</td>
  75. <td valign="middle" align="center" rowspan="3">One of these or
  76. nested text, when using the name attribute</td>
  77. </tr>
  78. <tr>
  79. <td valign="top">location</td>
  80. <td valign="top">Sets the property to the absolute filename of the
  81. given file. If the value of this attribute is an absolute path, it
  82. is left unchanged (with / and \ characters converted to the
  83. current platforms conventions). Otherwise it is taken as a path
  84. relative to the project's basedir and expanded.</td>
  85. </tr>
  86. <tr>
  87. <td valign="top">refid</td>
  88. <td valign="top"><a href="../using.html#references">Reference</a> to an object
  89. defined elsewhere. Only yields reasonable results for references
  90. to <a href="../using.html#path">PATH like structures</a> or properties.</td>
  91. </tr>
  92. <tr>
  93. <td valign="top">resource</td>
  94. <td valign="top"> the name of the classpath resource containing
  95. properties settings in properties file format.</td>
  96. <td valign="middle" align="center" rowspan="4">One of these, when
  97. <b>not</b> using the name attribute</td>
  98. </tr>
  99. <tr>
  100. <td valign="top">file</td>
  101. <td valign="top">the location of the properties file to load.</td>
  102. </tr>
  103. <tr>
  104. <td valign="top">url</td>
  105. <td valign="top">a url containing properties-format settings.</td>
  106. </tr>
  107. <tr>
  108. <td valign="top">environment</td>
  109. <td valign="top">the prefix to use when retrieving environment variables. Thus
  110. if you specify environment=&quot;myenv&quot; you will be able to access OS-specific
  111. environment variables via property names &quot;myenv.PATH&quot; or
  112. &quot;myenv.TERM&quot;. Note that if you supply a property name with a final
  113. &quot;.&quot; it will not be doubled; i.e. environment=&quot;myenv.&quot; will still
  114. allow access of environment variables through &quot;myenv.PATH&quot; and
  115. &quot;myenv.TERM&quot;. This functionality is currently only implemented
  116. on <a href="#notes-env">select platforms</a>. Feel free to send patches to increase the
  117. number of platforms on which this functionality is supported ;).<br>
  118. Note also that properties are case-sensitive, even if the
  119. environment variables on your operating system are not; e.g. Windows 2000's
  120. system path variable is set to an Ant property named "env.Path"
  121. rather than "env.PATH".</td>
  122. </tr>
  123. <tr>
  124. <td valign="top">classpath</td>
  125. <td valign="top">the classpath to use when looking up a resource.</td>
  126. <td align="center" valign="top">No</td>
  127. </tr>
  128. <tr>
  129. <td valign="top">classpathref</td>
  130. <td valign="top">the classpath to use when looking up a resource,
  131. given as <a href="../using.html#references">reference</a> to a <code>&lt;path&gt;</code> defined
  132. elsewhere..</td>
  133. <td align="center" valign="top">No</td>
  134. </tr>
  135. <tr>
  136. <td valign="top">prefix</td>
  137. <td valign="top">Prefix to apply to properties loaded using <code>file</code>,
  138. <code>resource</code>, or <code>url</code>.
  139. A "." is appended to the prefix if not specified.</td>
  140. <td align="center" valign="top">No</td>
  141. </tr>
  142. <tr>
  143. <td valign="top">prefixValues</td>
  144. <td valign="top">Whether to apply the prefix when expanding the
  145. right hand side of properties loaded using <code>file</code>,
  146. <code>resource</code>, or <code>url</code>.
  147. <em>Since Ant 1.8.2</em></td>
  148. <td align="center" valign="top">No (default=<tt>false</tt>)</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. <h4>OpenVMS Users</h4>
  164. <p>With the <code>environment</code> attribute this task will load all defined
  165. logicals on an OpenVMS system. Logicals with multiple equivalence names get
  166. mapped to a property whose value is a comma separated list of all equivalence
  167. names. If a logical is defined in multiple tables, only the most local
  168. definition is available (the table priority order being PROCESS, JOB, GROUP,
  169. SYSTEM).
  170. </p>
  171. <h4>Any OS except OpenVMS</h4>
  172. <p><em>Since Ant 1.8.2</em>, if Ant detects it is running on a Java 5
  173. or newer VM, Ant will use <code>System.getenv</code> rather than
  174. its own OS dependent native implementation. For some OSes this
  175. causes minor differences when compared to older versions of Ant.
  176. For a full list
  177. see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=49366">Bugzilla
  178. Issue 49366</a>. In particular:</p>
  179. <ul>
  180. <li>On Windows, Ant will now return additional "environment
  181. variables" that correspond to the drive specific current working
  182. directories when Ant is run from the command line. The keys of
  183. these variables starts with an equals sign.</li>
  184. <li>Some users reported that some Cygwin specific variables (in
  185. particular PROMPT) was no longer present.</li>
  186. <li>On OS/2, Ant no longer returns the BEGINLIBPATH variable.</li>
  187. </ul>
  188. <h3>Parameters specified as nested elements</h3>
  189. <h4>classpath</h4>
  190. <p><code>Property</code>'s <i>classpath</i> attribute is a <a
  191. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  192. <i>classpath</i> element.</p>
  193. <h3>Examples</h3>
  194. <pre> &lt;property name=&quot;foo.dist&quot; value=&quot;dist&quot;/&gt;</pre>
  195. <p>sets the property <code>foo.dist</code> to the value &quot;dist&quot;.</p>
  196. <pre> &lt;property name=&quot;foo.dist&quot;&gt;dist&lt;/property&gt;</pre>
  197. <p>sets the property <code>foo.dist</code> to the value &quot;dist&quot;.</p>
  198. <pre> &lt;property file=&quot;foo.properties&quot;/&gt;</pre>
  199. <p>reads a set of properties from a file called &quot;foo.properties&quot;.</p>
  200. <pre> &lt;property url=&quot;http://www.mysite.com/bla/props/foo.properties&quot;/&gt;</pre>
  201. <p>reads a set of properties from the address &quot;http://www.mysite.com/bla/props/foo.properties&quot;.</p>
  202. <pre> &lt;property resource=&quot;foo.properties&quot;/&gt;</pre>
  203. <p>reads a set of properties from a resource called &quot;foo.properties&quot;.</p>
  204. <p>Note that you can reference a global properties file for all of your Ant
  205. builds using the following:</p>
  206. <pre> &lt;property file=&quot;${user.home}/.ant-global.properties&quot;/&gt;</pre>
  207. <p>since the &quot;user.home&quot; property is defined by the Java virtual machine
  208. to be your home directory. Where the &quot;user.home&quot; property resolves to in
  209. the file system depends on the operating system version and the JVM implementation.
  210. On Unix based systems, this will map to the user's home directory. On modern Windows
  211. variants, this will most likely resolve to the user's directory in the &quot;Documents
  212. and Settings&quot; or &quot;Users&quot; folder. Older windows variants such as Windows 98/ME are less
  213. predictable, as are other operating system/JVM combinations.</p>
  214. <pre>
  215. &lt;property environment=&quot;env&quot;/&gt;
  216. &lt;echo message=&quot;Number of Processors = ${env.NUMBER_OF_PROCESSORS}&quot;/&gt;
  217. &lt;echo message=&quot;ANT_HOME is set to = ${env.ANT_HOME}&quot;/&gt;
  218. </pre>
  219. <p>reads the system environment variables and stores them in properties, prefixed with &quot;env&quot;.
  220. Note that this only works on <em>select</em> operating systems.
  221. Two of the values are shown being echoed.
  222. </p>
  223. <pre>
  224. &lt;property environment=&quot;env&quot;/&gt;
  225. &lt;property file=&quot;${user.name}.properties&quot;/&gt;
  226. &lt;property file=&quot;${env.STAGE}.properties&quot;/&gt;
  227. &lt;property file=&quot;build.properties&quot;/&gt;
  228. </pre>
  229. <p>This buildfile uses the properties defined in <tt>build.properties</tt>. Regarding to the
  230. environment variable <tt>STAGE</tt> some or all values could be overwritten, e.g. having
  231. <tt>STAGE=test</tt> and a <tt>test.properties</tt> you have special values for that (like another
  232. name for the test server). Finally all these values could be overwritten by personal settings with
  233. a file per user.</p>
  234. <pre>
  235. &lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;..&quot;/&gt;
  236. </pre>
  237. <p>Stores the relative path in <tt>foo</tt>: projectbasedir/my/file.txt</p>
  238. <pre>
  239. &lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;cvs&quot;/&gt;
  240. </pre>
  241. <p>Stores the relative path in <tt>foo</tt>: ../my/file.txt</p>
  242. <h3>Property Files</h3>
  243. As stated, this task will load in a properties file stored in the file
  244. system, or as a resource on a classpath. Here are some interesting facts
  245. about this feature
  246. <ol>
  247. <li>If the file is not there, nothing is printed except at -verbose log
  248. level. This lets you have optional configuration files for every
  249. project, that team members can customize.
  250. <li>The rules for this format match <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.InputStream%29">java.util.Properties</a>.</li>
  251. <li>Trailing spaces are not stripped. It may have been what you wanted.</li>
  252. <li>Want unusual characters? Escape them \u0456 or \&quot; style.</li>
  253. <li>Ant Properties are expanded in the file</li>
  254. <li>If you want to expand properties defined inside the same file and
  255. you use the prefix attribute of the task, you must use the same
  256. prefix when expanding the properties or
  257. set <code>prefixValues</code> to true.</li>
  258. </ol>
  259. In-file property expansion is very cool. Learn to use it.
  260. <p>
  261. Example:
  262. <pre>
  263. build.compiler=jikes
  264. deploy.server=lucky
  265. deploy.port=8080
  266. deploy.url=http://${deploy.server}:${deploy.port}/
  267. </pre>
  268. <h3 id="notes-env">Notes about environment variables</h3>
  269. <p>
  270. Ant runs on Java 1.2 therefore it cannot use Java 5 features for accessing environment
  271. variables. So it starts a command in a new process which prints the environment variables,
  272. analyzes the output and creates the properties.<br>
  273. There are commands for the following operating systems implemented in
  274. <a href="https://git-wip-us.apache.org/repos/asf?p=ant.git;a=blob;f=src/main/org/apache/tools/ant/taskdefs/Execute.java;hb=24e5a0e881dba01a6f012c4a271b743946412a0d">
  275. Execute.java</a> (method <tt>getProcEnvCommand()</tt>):
  276. </p>
  277. <table>
  278. <tr>
  279. <th>OS</th>
  280. <th>command</th>
  281. </tr>
  282. <tr>
  283. <td>os/2</td>
  284. <td>cmd /c set</td>
  285. </tr>
  286. <tr>
  287. <td colspan="2"> windows</td>
  288. </tr>
  289. <tr>
  290. <td>* win9x</td>
  291. <td>command.com /c set</td>
  292. </tr>
  293. <tr>
  294. <td>* other</td>
  295. <td>cmd /c set</td>
  296. </tr>
  297. <tr>
  298. <td>z/os</td>
  299. <td>/bin/env <b>OR</b> /usr/bin/env <b>OR</b> env <i>(depending on read rights)</i></td>
  300. </tr>
  301. <tr>
  302. <td>unix</td>
  303. <td>/bin/env <b>OR</b> /usr/bin/env <b>OR</b> env <i>(depending on read rights)</i></td>
  304. </tr>
  305. <tr>
  306. <td>netware</td>
  307. <td>env</td>
  308. </tr>
  309. <tr>
  310. <td>os/400</td>
  311. <td>env</td>
  312. </tr>
  313. <tr>
  314. <td>openvms</td>
  315. <td>show logical</td>
  316. </tr>
  317. </table>
  318. </body>
  319. </html>