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.

properties.html 18 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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>Properties and PropertyHelpers</title>
  20. </head>
  21. <body>
  22. <h1>Properties</h1>
  23. <p>Properties are key-value pairs where Apache Ant tries to expand <code>${key}</code>
  24. to <code>value</code> at run time.</p>
  25. <p>There are many tasks that can set properties; the most common one is
  26. the <a href="Tasks/property.html">property</a> task. In addition properties can be defined
  27. via <a href="running.html">command line arguments</a> or similar mechanisms from outside of
  28. Ant.</p>
  29. <p>Normally property values can not be changed: once a property is set, most tasks will not allow
  30. its value to be modified. In general properties are of global scope, i.e., once they have been
  31. defined they are available for any task or target invoked subsequently&mdash;it is not possible
  32. to set a property in a child build process created via
  33. the <a href="Tasks/ant.html">ant</a>, <a href="Tasks/antcall.html">antcall</a>
  34. or <a href="Tasks/subant.html">subant</a> tasks and make it available to the calling build
  35. process, though.</p>
  36. <p><em>Since Ant 1.8.0</em> the <a href="Tasks/local.html">local</a> task can be used to create
  37. properties that are locally scoped to a target or
  38. a <a href="Tasks/sequential.html">sequential</a> element like the one of
  39. the <a href="Tasks/macrodef.html">macrodef</a> task.</p>
  40. <h2 id="built-in-props">Built-in Properties</h2>
  41. <p>Ant provides access to all system properties as if they had been defined using
  42. a <code>&lt;property&gt;</code> task. For example, <samp>${os.name}</samp> expands to the name
  43. of the operating system.</p>
  44. <p>For a list of system properties,
  45. see <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties--"
  46. target="_top">the javadoc of System.getProperties</a>.
  47. </p>
  48. <p>In addition, Ant has some built-in properties:</p>
  49. <dl>
  50. <dt><code>basedir</code></dt>
  51. <dd>the absolute path of the project's basedir (as set with
  52. the <var>basedir</var> attribute
  53. of <a href="using.html#projects">&lt;project&gt;</a>).</dd>
  54. <dt><code>ant.file</code></dt>
  55. <dd>the absolute path of the buildfile.</dd>
  56. <dt><code>ant.version</code></dt>
  57. <dd>the version of Ant</dd>
  58. <dt><code>ant.project.name</code></dt>
  59. <dd>the name of the project that is currently executing; it is set in the <var>name</var>
  60. attribute of <code>&lt;project&gt;</code>.</dd>
  61. <dt><code>ant.project.default-target</code></dt>
  62. <dd>the name of the currently executing project's default target; it is set via
  63. the <var>default</var> attribute of <code>&lt;project&gt;</code>.</dd>
  64. <dt><code>ant.project.invoked-targets</code></dt>
  65. <dd>a comma-separated list of the targets that have been specified (on the command line, inside
  66. of an IDE, by an <code>&lt;ant&gt;</code> task, etc...) when invoking the current
  67. project.<br/>This property is set properly when the first target is executed. If you use it in
  68. the implicit target (directly under the <code>&lt;project&gt;</code> tag) the list will be
  69. empty if no target has been specified while it will contain the project's default target in
  70. this case for tasks nested into targets.</dd>
  71. <dt><code>ant.java.version</code></dt>
  72. <dd>the JVM version Ant detected; currently it can hold the
  73. values <q>9</q>, <q>1.8</q>, <q>1.7</q>, <q>1.6</q>, <q>1.5</q>, <q>1.4</q>, <q>1.3</q>
  74. and <q>1.2</q>.</dd>
  75. <dt><code>ant.core.lib</code></dt>
  76. <dd>the absolute path of the <samp>ant.jar</samp> file.</dd>
  77. </dl>
  78. <p>There is also another property, but this is set by the launcher script and therefore maybe not
  79. set inside IDEs:</p>
  80. <dl>
  81. <dt><code>ant.home</code></dt>
  82. <dd>home directory of Ant</dd>
  83. </dl>
  84. <p>The following property is only set if Ant is started via the Launcher class (which means it may
  85. not be set inside IDEs either):</p>
  86. <dl>
  87. <dt><code>ant.library.dir</code></dt>
  88. <dd>the directory that has been used to load Ant's jars from. In most cases this
  89. is <samp>ANT_HOME/lib</samp>.</dd>
  90. </dl>
  91. <h1 id="propertyHelper">PropertyHelpers</h1>
  92. <p>Ant's property handling is accomplished by an instance
  93. of <code class="code">org.apache.tools.ant.PropertyHelper</code> associated with the current
  94. Project. You can learn more about this class by examining Ant's Java API. In Ant 1.8 the
  95. <code class="code">PropertyHelper</code> class was much reworked and now itself employs a number
  96. of helper classes (actually instances of
  97. the <code class="code">org.apache.tools.ant.PropertyHelper$Delegate</code> marker interface) to
  98. take care of discrete tasks such as property setting, retrieval, parsing, etc. This makes Ant's
  99. property handling highly extensible; also of interest is the
  100. new <a href="Tasks/propertyhelper.html">propertyhelper</a> task used to manipulate the
  101. PropertyHelper and its delegates from the context of the Ant buildfile.</p>
  102. <p>There are three sub-interfaces of <code class="code">Delegate</code> that may be useful to
  103. implement:</p>
  104. <ul>
  105. <li><code>org.apache.tools.ant.property.PropertyExpander</code> is responsible for finding the
  106. property name inside a string in the first place (the default extracts <samp>foo</samp>
  107. from <samp>${foo}</samp>).
  108. <p>This is the interface you'd implement if you wanted to invent your own property
  109. syntax&mdash;or allow nested property expansions since the default implementation doesn't
  110. balance braces
  111. (see <a href="https://git-wip-us.apache.org/repos/asf?p=ant-antlibs-props.git;a=blob;f=src/main/org/apache/ant/props/NestedPropertyExpander.java;hb=HEAD"
  112. target="_top"><code class="code">NestedPropertyExpander</code> in the <samp>props</samp> Antlib</a> for
  113. an example).</p>
  114. </li>
  115. <li><code class="code">org.apache.tools.ant.PropertyHelper$PropertyEvaluator</code> is used to
  116. expand <samp>${some-string}</samp> into an <code>Object</code>.
  117. <p>This is the interface you'd implement if you want to provide your own storage independent
  118. of Ant's project instance&mdash;the interface represents the reading end. An example for
  119. this would be <code class="code">org.apache.tools.ant.property.LocalProperties</code> which
  120. implements storage for <a href="Tasks/local.html">local properties</a>.</p>
  121. <p>Another reason to implement this interface is if you wanted to provide your own "property
  122. protocol" like expanding <code>toString:foo</code> by looking up the project
  123. reference <samp>foo</samp> and invoking <code class="code">toString()</code> on it (which is
  124. already implemented in Ant, see below).</p>
  125. </li>
  126. <li><code class="code">org.apache.tools.ant.PropertyHelper$PropertySetter</code> is responsible
  127. for setting properties.
  128. <p>This is the interface you'd implement if you want to provide your own storage independent
  129. of Ant's project instance&mdash;the interface represents the writing end. An example for
  130. this would be <code class="code">org.apache.tools.ant.property.LocalProperties</code> which
  131. implements storage for <a href="Tasks/local.html">local properties</a>.</p>
  132. </li>
  133. </ul>
  134. <p>The default <code class="code">PropertyExpander</code> looks similar to:</p>
  135. <pre>
  136. public class DefaultExpander implements PropertyExpander {
  137. public String parsePropertyName(String s, ParsePosition pos,
  138. ParseNextProperty notUsed) {
  139. int index = pos.getIndex();
  140. if (s.indexOf("${", index) == index) {
  141. int end = s.indexOf('}', index);
  142. if (end < 0) {
  143. throw new BuildException("Syntax error in property: " + s);
  144. }
  145. int start = index + 2;
  146. pos.setIndex(end + 1);
  147. return s.substring(start, end);
  148. }
  149. return null;
  150. }
  151. }</pre>
  152. <p>The logic that replaces <samp>${toString:<i>some-id</i>}</samp> with the stringified
  153. representation of the object with <var>id</var> <samp><i>some-id</i></samp> inside the current
  154. build is contained in a <code class="code">PropertyEvaluator</code> similar to the following
  155. code:</p>
  156. <pre>
  157. public class ToStringEvaluator implements PropertyHelper.PropertyEvaluator {
  158. private static final String prefix = "toString:";
  159. public Object evaluate(String property, PropertyHelper propertyHelper) {
  160. Object o = null;
  161. if (property.startsWith(prefix) && propertyHelper.getProject() != null) {
  162. o = propertyHelper.getProject().getReference(
  163. property.substring(prefix.length()));
  164. }
  165. return o == null ? null : o.toString();
  166. }
  167. }</pre>
  168. <h1>Property Expansion</h1>
  169. <p>When Ant encounters a construct <samp>${some-text}</samp> the exact parsing semantics are
  170. subject to the configured property helper delegates.</p>
  171. <h2><code>$$</code> Expansion</h2>
  172. <p>In its default configuration Ant will expand the text <q>$$</q> to a single <q>$</q> and
  173. suppress the normal property expansion mechanism for the text immediately following it,
  174. i.e., <samp>$${key}</samp> expands to <samp>${key}</samp> and not <code>value</code> even though
  175. a property named <code>key</code> was defined and had the value <samp>value</samp>. This can be
  176. used to escape literal <q>$</q> characters and is useful in constructs that only look like
  177. property expansions or when you want to provide diagnostic output like in</p>
  178. <pre>&lt;echo&gt;$${builddir}=${builddir}&lt;/echo&gt;</pre>
  179. <p>which will echo this message:</p>
  180. <pre class="output">${builddir}=build/classes</pre>
  181. <p>if the property <code>builddir</code> has the value <samp>build/classes</samp>.</p>
  182. <p>In order to maintain backward compatibility with older Ant releases, a single <q>$</q>
  183. character encountered apart from a property-like construct (including a matched pair of french
  184. braces) will be interpreted literally, that is, as <q>$</q>. The "correct" way to specify this
  185. literal character, however, is by using the escaping mechanism unconditionally, so
  186. that <q>$$</q> is obtained by specifying <q>$$$$</q>. Mixing the two approaches yields
  187. unpredictable results, as <q>$$$</q> results in <q>$$</q>.</p>
  188. <h2>Nesting of Braces</h2>
  189. <p>In its default configuration Ant will not try to balance braces in property expansions, it will
  190. only consume the text up to the first closing brace when creating a property name. I.e. when
  191. expanding something like <samp>${a${b}}</samp> it will be translated into two parts:</p>
  192. <ol>
  193. <li>the expansion of property <samp>a${b</samp>&mdash;likely nothing useful.</li>
  194. <li>the literal text <samp>}</samp> resulting from the second closing brace</li>
  195. </ol>
  196. <p>This means you can't use easily expand properties whose names are stored in properties, but
  197. there are <a href="https://ant.apache.org/faq.html#propertyvalue-as-name-for-property"
  198. target="_top">some workarounds</a> for older versions of Ant. <em>Since Ant 1.8.0</em> using
  199. the <a href="https://ant.apache.org/antlibs/props/" target="_top">props Antlib</a> you can
  200. configure Ant to use the <code class="code">NestedPropertyExpander</code> defined there if you
  201. need such a feature.</p>
  202. <h2>Expanding a Property Reference</h2>
  203. <p>In its most simple form <samp>${key}</samp> is supposed to look up a property
  204. named <code>key</code> and expand to the value of the property.
  205. Additional <code>PropertyEvaluator</code>s may result in a different interpretation
  206. of <code>key</code>, though.</p>
  207. <p>The <a href="https://ant.apache.org/antlibs/props/" target="_top">props Antlib</a> provides a
  208. few interesting evaluators but there are also a few built-in ones.</p>
  209. <h3 id="toString">Getting the value of a Reference with <samp>${toString:}</samp></h3>
  210. <p>Any Ant type item which has been declared with a reference can also its string value extracted
  211. by using the <samp>${toString:}</samp> operation, with the name of the reference listed after
  212. the <code>toString:</code> text. The <code class="code">toString()</code> method of the Java
  213. class instance that is referenced is invoked&mdash;all built in types strive to produce useful
  214. and relevant output in such an instance.</p>
  215. <p>For example, here is how to get a listing of the files in a fileset:<p>
  216. <pre>
  217. &lt;fileset id=&quot;sourcefiles&quot; dir=&quot;src&quot; includes=&quot;**/*.java&quot;/&gt;
  218. &lt;echo&gt; sourcefiles = ${toString:sourcefiles} &lt;/echo&gt;</pre>
  219. <p>There is no guarantee that external types provide meaningful information in such a
  220. situation</p>
  221. <h3 id="ant.refid">Getting the value of a Reference with <samp>${ant.refid:}</samp></h3>
  222. <p>Any Ant type item which has been declared with a reference can also be used as a property by
  223. using the <samp>${ant.refid:}</samp> operation, with the name of the reference listed after
  224. the <code>ant.refid:</code> text. The difference between this operation
  225. and <a href="#toString"><samp>${toString:}</samp></a> is that <samp>${ant.refid:}</samp> will
  226. expand to the referenced object itself. In most circumstances
  227. the <code class="code">toString()</code> method will be invoked anyway, for example if
  228. the <samp>${ant.refid:}</samp> is surrounded by other text.</p>
  229. <p>This syntax is most useful when using a task with attribute setters that accept objects other
  230. than <code class="code">String</code>. For example, if the setter accepts
  231. a <code class="code">Resource</code> object as in</p>
  232. <pre>public void setAttr(Resource r) { ... }</pre>
  233. <p>then the syntax can be used to pass in resource subclasses previously defined as references
  234. like</p>
  235. <pre>
  236. &lt;url url="http://ant.apache.org/" id="anturl"/&gt;
  237. &lt;my:task attr="${ant.refid:anturl}"/&gt;</pre>
  238. <h2 id="if+unless">If/Unless Attributes</h2>
  239. <p>
  240. The <code>&lt;target&gt;</code> element and various tasks (such as <code>&lt;fail&gt;</code>)
  241. and task elements (such as <code>&lt;test&gt;</code> in <code>&lt;junit&gt;</code>)
  242. support <var>if</var> and <var>unless</var> attributes which can be used to control whether the
  243. item is run or otherwise takes effect.
  244. </p>
  245. <p>
  246. In Ant 1.7.1 and earlier, these attributes could only be property names. The item was enabled
  247. if a property with that name was defined&mdash;even to be the empty string
  248. or <q>false</q>&mdash;and disabled if the property was not defined. For example, the following
  249. works but there is no way to override the file existence check negatively (only positively):
  250. </p>
  251. <pre>
  252. &lt;target name="-check-use-file"&gt;
  253. &lt;available property="file.exists" file="some-file"/&gt;
  254. &lt;/target&gt;
  255. &lt;target name="use-file" depends="-check-use-file" <strong>if="file.exists"</strong>&gt;
  256. &lt;!-- do something requiring that file... --&gt;
  257. &lt;/target>
  258. &lt;target name="lots-of-stuff" depends="use-file,other-unconditional-stuff"/&gt;</pre>
  259. <p>
  260. <em>Since Ant 1.8.0</em>, you may instead use property expansion; a value of <q>true</q>
  261. (or <q>on</q> or <q>yes</q>) will enable the item, while <q>false</q> (or <q>off</q>
  262. or <q>no</q>) will disable it. Other values are still assumed to be property names and so the
  263. item is enabled only if the named property is defined.
  264. </p>
  265. <p>
  266. Compared to the older style, this gives you additional flexibility, because you can override the
  267. condition from the command line or parent scripts:
  268. </p>
  269. <pre>
  270. &lt;target name="-check-use-file" <strong>unless="file.exists"</strong>&gt;
  271. &lt;available property="file.exists" file="some-file"/&gt;
  272. &lt;/target>
  273. &lt;target name="use-file" depends="-check-use-file" <strong>if="${file.exists}"</strong>&gt;
  274. &lt;!-- do something requiring that file... --&gt;
  275. &lt;/target>
  276. &lt;target name="lots-of-stuff" depends="use-file,other-unconditional-stuff"/&gt;</pre>
  277. <p>
  278. Now <kbd>ant -Dfile.exists=false lots-of-stuff</kbd> will run <q>other-unconditional-stuff</q>
  279. but not <q>use-file</q>, as you might expect, and you can disable the condition from another
  280. script too:
  281. </p>
  282. <pre>
  283. &lt;antcall target="lots-of-stuff"&gt;
  284. &lt;param name="file.exists" value="false"/&gt;
  285. &lt;/antcall&gt;</pre>
  286. <p>
  287. Similarly, an <var>unless</var> attribute disables the item if it is either the name of property
  288. which is defined, or if it evaluates to a <q>true</q>-like value. For example, the following
  289. allows you to define <code>skip.printing.message=true</code> in <samp>my-prefs.properties</samp>
  290. with the results you might expect:
  291. </p>
  292. <pre>
  293. &lt;property file="my-prefs.properties"/&gt;
  294. &lt;target name="print-message" <strong>unless="${skip.printing.message}"</strong>&gt;
  295. &lt;echo>hello!&lt;/echo>
  296. &lt;/target&gt;</pre>
  297. </body>
  298. </html>