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

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