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 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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> (by name and value), or set of properties
  25. (from file or resource) in the project. Properties are case sensitive.</p>
  26. <p>Properties are immutable: whoever sets a property first freezes it for the rest of the build;
  27. they are most definitely not variables.</p>
  28. <p>There are seven ways to set properties:</p>
  29. <ul>
  30. <li>By supplying both the <var>name</var> and one of <var>value</var> or <var>location</var>
  31. attributes.</li>
  32. <li>By supplying the <var>name</var> and nested text.</li>
  33. <li>By supplying both the <var>name</var> and <var>refid</var> attributes.</li>
  34. <li>By setting the <var>file</var> attribute with the filename of the property file to load. This
  35. property file has the format as defined by the file used in the
  36. class <code class="code">java.util.Properties</code>, with the same rules about how
  37. non-ISO-8859-1 characters must be escaped.</li>
  38. <li>By setting the <var>url</var> attribute with the URL from which to load the properties. This
  39. URL must be directed to a file that has the format as defined by the file used in the
  40. class <code class="code">java.util.Properties</code>.</li>
  41. <li>By setting the <var>resource</var> attribute with the resource name of the property file to
  42. load. A resource is a property file on the current classpath, or on the specified
  43. classpath.</li>
  44. <li>By setting the <var>environment</var> attribute with a prefix to use. Properties will be
  45. defined for every environment variable by prefixing the supplied name and a period to the name
  46. of the variable.</li>
  47. <li>By setting the <i>runtime</i> attribute with a prefix to use.
  48. Properties <code>prefix.availableProcessors</code>,
  49. <code>prefix.freeMemory</code>, <code>prefix.totalMemory</code>
  50. and <code>prefix.maxMemory</code> will be defined with values that
  51. correspond to the corresponding methods of
  52. the <a href="https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html">Runtime</a>
  53. class.</li>
  54. </ul>
  55. <p>Although combinations of these ways are possible, only one should be used at a time. Problems
  56. might occur with the order in which properties are set, for instance.</p>
  57. <p>The value part of the properties being set might contain references to other properties. These
  58. references are resolved at the time these properties are set. This also holds for properties loaded
  59. from a property file.</p>
  60. <p>A list of predefined properties can be
  61. found <a href="../properties.html#built-in-props">here</a>.</p>
  62. <p><em>Since Apache Ant 1.8.0</em>, it is possible to load properties defined in XML according
  63. to <a href="http://java.sun.com/dtd/properties.dtd" target="_top">Sun DTD</a>, when running on Java
  64. 5+. For this the name of the file, resource or url has to end with <samp>.xml</samp>.</p>
  65. <h3>Parameters</h3>
  66. <table class="attr">
  67. <tr>
  68. <th>Attribute</th>
  69. <th>Description</th>
  70. <th>Required</th>
  71. </tr>
  72. <tr>
  73. <td>name</td>
  74. <td>the name of the property to set.</td>
  75. <td>No</td>
  76. </tr>
  77. <tr>
  78. <td>value</td>
  79. <td>the value of the property.</td>
  80. <td rowspan="3">One of these or nested text, when the <var>name</var> attribute is set</td>
  81. </tr>
  82. <tr>
  83. <td>location</td>
  84. <td class="left">Sets the property to the absolute filename of the given file. If the value of
  85. this attribute is an absolute path, it is left unchanged (with <q>/</q> and <q>\</q>
  86. characters converted to the current platforms conventions). Otherwise it is taken as a path
  87. relative to the project's <var>basedir</var> and expanded.</td>
  88. </tr>
  89. <tr>
  90. <td>refid</td>
  91. <td class="left"><a href="../using.html#references">Reference</a> to an object defined
  92. elsewhere. Only yields reasonable results for references
  93. to <a href="../using.html#path">path-like structures</a> or properties.</td>
  94. </tr>
  95. <tr>
  96. <td>resource</td>
  97. <td>the name of the classpath resource containing properties settings in properties file
  98. format.</td>
  99. <td rowspan="4">One of these, <strong>unless</strong> the <var>name</var> attribute is set</td>
  100. </tr>
  101. <tr>
  102. <td>file</td>
  103. <td class="left">the location of the properties file to load.</td>
  104. </tr>
  105. <tr>
  106. <td>url</td>
  107. <td class="left">a URL containing properties-format settings.</td>
  108. </tr>
  109. <tr>
  110. <td>environment</td>
  111. <td class="left">the prefix to use when retrieving environment variables. Thus if you
  112. specify <var>environment</var>=<q>myenv</q> you will be able to access OS-specific environment
  113. variables via property names <code>myenv.PATH</code> or <code>myenv.TERM</code>. Note that if
  114. you supply a property <var>name</var> with a final <q>.</q> it will not be doubled;
  115. i.e. <var>environment</var>=<q>myenv.</q> will still allow access of environment variables
  116. through <code>myenv.PATH</code> and <code>myenv.TERM</code>. This functionality is currently
  117. only implemented on <a href="#notes-env">select platforms</a>. Feel free to send patches to
  118. increase the number of platforms on which this functionality is supported ;-).<br/>Note also
  119. that properties are case-sensitive, even if the environment variables on your operating system
  120. are not; e.g. Windows 2000 or later system path variable is set to an Ant property
  121. named <code>env.Path</code> rather than <code>env.PATH</code>.</td>
  122. </tr>
  123. <tr>
  124. <td>runtime</td>
  125. <td>the prefix to use when retrieving Runtime properties. Thus if
  126. you specify <var>runtime</var>=<q>myrt</q> you will be able to
  127. access runtime values corresponding to methods in
  128. the <a href="https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html">Runtime</a>
  129. class via property names <code>myrt.availableProcessors</code>,
  130. <code>myrt.maxMemory</code>, <code>myrt.totalMemory</code> or
  131. <code>myrt.freeMemory</code>. Note that if you supply a property name with a final
  132. <code>.</code> it will not be doubled; i.e. runtime=<code>myrt.</code> will still
  133. allow access of prpperties through <code>myrt.maxMemory</code>.<br>
  134. Note also that the property values are snapshots taken at the point in time
  135. when the <code>property</code> has been executed.
  136. <em>Since Ant 1.10.4</em>
  137. </td>
  138. </tr>
  139. <tr>
  140. <td>classpath</td>
  141. <td>the classpath to use when looking up a resource.</td>
  142. <td>No</td>
  143. </tr>
  144. <tr>
  145. <td>classpathref</td>
  146. <td>the classpath to use when looking up a resource, given
  147. as <a href="../using.html#references">reference</a> to a <code>&lt;path&gt;</code> defined
  148. elsewhere..</td>
  149. <td>No</td>
  150. </tr>
  151. <tr>
  152. <td>prefix</td>
  153. <td>Prefix to apply to properties loaded using <var>file</var>, <var>resource</var>,
  154. or <var>url</var>. A <q>.</q> is appended to the prefix if not specified.</td>
  155. <td>No</td>
  156. </tr>
  157. <tr>
  158. <td>prefixValues</td>
  159. <td>Whether to apply the prefix when expanding the right hand side of properties loaded
  160. using <var>file</var>, <var>resource</var>, or <var>url</var>. <em>Since Ant 1.8.2</em></td>
  161. <td>No; default is <q>false</q></td>
  162. </tr>
  163. <tr>
  164. <td>relative</td>
  165. <td>If set to <q>true</q> the relative path to <var>basedir</var> is set. <em>Since Ant
  166. 1.8.0</em></td>
  167. <td>No; default is <q>false</q></td>
  168. </tr>
  169. <tr>
  170. <td>basedir</td>
  171. <td>The <var>basedir</var> to calculate the relative path from. <em>Since Ant 1.8.0</em></td>
  172. <td>No; default is project's <var>basedir</var></td>
  173. </tr>
  174. </table>
  175. <h4>OpenVMS Users</h4>
  176. <p>With the <var>environment</var> attribute this task will load all defined logicals on an OpenVMS
  177. system. Logicals with multiple equivalence names get mapped to a property whose value is a comma
  178. separated list of all equivalence names. If a logical is defined in multiple tables, only the most
  179. local definition is available (the table priority order being PROCESS, JOB, GROUP, SYSTEM).</p>
  180. <h4>Any OS except OpenVMS</h4>
  181. <p><em>Since Ant 1.8.2</em>, if Ant detects it is running on a Java 5 or newer, Ant will
  182. use <code class="code">System.getenv()</code> rather than its own OS dependent native
  183. implementation. For some OSes this causes minor differences when compared to older versions of Ant.
  184. For a full list see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=49366"
  185. target="_top">Bugzilla Issue 49366</a>. In particular:</p>
  186. <ul>
  187. <li>On Windows, Ant will now return additional "environment variables" that correspond to the
  188. drive specific current working directories when Ant is run from the command line. The keys of
  189. these variables starts with an equals sign.</li>
  190. <li>Some users reported that some Cygwin specific variables (in particular <code>PROMPT</code>)
  191. were no longer present.</li>
  192. <li>On OS/2, Ant no longer returns the <code>BEGINLIBPATH</code> variable.</li>
  193. </ul>
  194. <h3>Parameters specified as nested elements</h3>
  195. <h4>classpath</h4>
  196. <p><code>Property</code>'s <var>classpath</var> attribute is
  197. a <a href="../using.html#path">path-like structure</a> and can also be set via a
  198. nested <code>classpath</code> element.</p>
  199. <h3>Examples</h3>
  200. <pre>&lt;property name=&quot;foo.dist&quot; value=&quot;dist&quot;/&gt;</pre>
  201. <p>sets the property <code>foo.dist</code> to the value <q>dist</q>.</p>
  202. <pre>&lt;property name=&quot;foo.dist&quot;&gt;dist&lt;/property&gt;</pre>
  203. <p>sets the property <code>foo.dist</code> to the value <q>dist</q>.</p>
  204. <pre>&lt;property file=&quot;foo.properties&quot;/&gt;</pre>
  205. <p>reads a set of properties from a file called <samp>foo.properties</samp>.</p>
  206. <pre>&lt;property url=&quot;http://www.mysite.com/bla/props/foo.properties&quot;/&gt;</pre>
  207. <p>reads a set of properties from the
  208. address <samp>http://www.mysite.com/bla/props/foo.properties</samp>.</p>
  209. <pre>&lt;property resource=&quot;foo.properties&quot;/&gt;</pre>
  210. <p>reads a set of properties from a resource called <samp>foo.properties</samp>.</p>
  211. <p>Note that you can reference a global properties file for all of your Ant builds using the
  212. following:</p>
  213. <pre>&lt;property file=&quot;${user.home}/.ant-global.properties&quot;/&gt;</pre>
  214. <p>since the <code>user.home</code> property is defined by JVM to be your home directory. Where
  215. the <code>user.home</code> property resolves to in the file system depends on the operating system
  216. version and the JVM implementation. On Unix based systems, this will map to the user's home
  217. directory. On modern Windows variants, this will most likely resolve to the user's directory in
  218. the <samp>Documents and Settings</samp> or <samp>Users</samp> folder. Older Windows variants such as
  219. Windows 98/ME are less predictable, as are other operating system/JVM combinations.</p>
  220. <pre>
  221. &lt;property environment=&quot;env&quot;/&gt;
  222. &lt;echo message=&quot;Number of Processors = ${env.NUMBER_OF_PROCESSORS}&quot;/&gt;
  223. &lt;echo message=&quot;ANT_HOME is set to = ${env.ANT_HOME}&quot;/&gt;</pre>
  224. <p>reads the system environment variables and stores them in properties, prefixed with <q>env</q>.
  225. Note that this only works on <em>select</em> operating systems. Two of the values are shown being
  226. echoed.
  227. </p>
  228. <pre>
  229. &lt;property environment=&quot;env&quot;/&gt;
  230. &lt;property file=&quot;${user.name}.properties&quot;/&gt;
  231. &lt;property file=&quot;${env.STAGE}.properties&quot;/&gt;
  232. &lt;property file=&quot;build.properties&quot;/&gt;</pre>
  233. <p>This buildfile uses the properties defined in <samp>build.properties</samp>. Regarding to the
  234. environment variable <code>STAGE</code> some or all values could be overwritten,
  235. e.g. having <code>STAGE=test</code> and a <samp>test.properties</samp> you have special values for
  236. that (like another name for the test server). Finally all these values could be overwritten by
  237. personal settings with a file per user.</p>
  238. <pre>&lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;..&quot;/&gt;</pre>
  239. <p>Stores the relative path in <code>foo</code>: <samp>../my/file.txt</samp></p>
  240. <pre>&lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;cvs&quot;/&gt;</pre>
  241. <p>Stores the relative path in <code>foo</code>: <samp>cvs/my/file.txt</samp></p>
  242. <h3>Property files</h3>
  243. <p>As stated, this task will load in a properties file stored in the file system, or as a resource
  244. on a classpath. Here are some interesting facts about this feature</p>
  245. <ol>
  246. <li>If the file is not there, nothing is printed except at <kbd>-verbose</kbd> log level. This
  247. lets you have optional configuration files for every project, that team members can customize.
  248. <li>The rules for this format
  249. match <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.InputStream-"
  250. target="_top">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 <code>\u0456</code> or <code>\&quot;</code> 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 you use
  255. the <var>prefix</var> attribute of the task, you must use the same prefix when expanding the
  256. properties or set <var>prefixValues</var> to <q>true</q>.</li>
  257. </ol>
  258. <p>In-file property expansion is very cool. Learn to use it.</p>
  259. <p>Example:</p>
  260. <pre>
  261. build.compiler=jikes
  262. deploy.server=lucky
  263. deploy.port=8080
  264. deploy.url=http://${deploy.server}:${deploy.port}/</pre>
  265. <h3 id="notes-env">Notes about environment variables</h3>
  266. <p>Ant runs on Java 1.2 therefore it cannot use Java 5 features for accessing environment
  267. variables. So it starts a command in a new process which prints the environment variables, analyzes
  268. the output and creates the properties.<br/> There are commands for the following operating systems
  269. implemented
  270. in <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=refs/heads/master"
  271. target="_top">Execute.java</a> (method <code class="code">getProcEnvCommand()</code>):
  272. </p>
  273. <table>
  274. <tr>
  275. <th>OS</th>
  276. <th>command</th>
  277. </tr>
  278. <tr>
  279. <td>os/2</td>
  280. <td><code>cmd /c set</code></td>
  281. </tr>
  282. <tr>
  283. <td colspan="2">windows</td>
  284. </tr>
  285. <tr>
  286. <td>* win9x</td>
  287. <td><code>command.com /c set</code></td>
  288. </tr>
  289. <tr>
  290. <td>* other</td>
  291. <td><code>cmd /c set</code></td>
  292. </tr>
  293. <tr>
  294. <td>z/os</td>
  295. <td><code>/bin/env</code> <strong>OR</strong> <code>/usr/bin/env</code> <strong>OR</strong> <code>env</code> (<em>depending on read rights</em>)</td>
  296. </tr>
  297. <tr>
  298. <td>unix</td>
  299. <td><code>/bin/env</code> <strong>OR</strong> <code>/usr/bin/env</code> <strong>OR</strong> <code>env</code> (<em>depending on read rights</em>)</td>
  300. </tr>
  301. <tr>
  302. <td>netware</td>
  303. <td><code>env</code></td>
  304. </tr>
  305. <tr>
  306. <td>os/400</td>
  307. <td><code>env</code></td>
  308. </tr>
  309. <tr>
  310. <td>openvms</td>
  311. <td><code>show logical</code></td>
  312. </tr>
  313. </table>
  314. </body>
  315. </html>