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 kB

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