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 kB

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