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 17 KiB

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