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

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