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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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="CoreTasks/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="CoreTasks/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="CoreTasks/local.html">local</a> task can be used to
  40. create properties that are locally scoped to a target or
  41. a <a href="CoreTasks/sequential.html">sequential</a> element like
  42. the one of the <a href="CoreTasks/macrodef.html">macrodef</a>
  43. task.</p>
  44. <a name="built-in-props"><h2>Built-in Properties</h2></a>
  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>
  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. <a name="propertyHelper"><h1>PropertyHelpers</h1></a>
  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="CoreTasks/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="CoreTasks/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="CoreTasks/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(property.substring(prefix.length()));
  170. }
  171. return o == null ? null : o.toString();
  172. }
  173. }
  174. </pre>
  175. <h1>Property Expansion</h1>
  176. <p>When Ant encounters a construct <code>${some-text}</code> the
  177. exact parsing semantics are subject to the configured property
  178. helper delegates.</p>
  179. <h2><code>$$</code> Expansion</h2>
  180. <p>In its default configuration Ant will expand the
  181. text <code>$$</code> to a single <code>$</code> and suppress the
  182. normal property expansion mechanism for the text immediately
  183. following it, i.e. <code>$${key}</code> expands
  184. to <code>${key}</code> and not <code>value</code> even though a
  185. property named <code>key</code> was defined and had the
  186. value <code>value</code>. This can be used to escape
  187. literal <code>$</code> characters and is useful in constructs that
  188. only look like property expansions or when you want to provide
  189. diagnostic output like in</p>
  190. <pre> &lt;echo&gt;$${builddir}=${builddir}&lt;/echo&gt;</pre>
  191. <p>which will echo this message:</p>
  192. <pre> ${builddir}=build/classes</pre>
  193. <p>if the property <code>builddir</code> has the
  194. value <code>build/classes</code>.</p>
  195. <p>In order to maintain backward compatibility with older Ant
  196. releases, a single '$' character encountered apart from a
  197. property-like construct (including a matched pair of french
  198. braces) will be interpreted literally; that is, as '$'. The
  199. "correct" way to specify this literal character, however, is by
  200. using the escaping mechanism unconditionally, so that "$$" is
  201. obtained by specifying "$$$$". Mixing the two approaches yields
  202. unpredictable results, as "$$$" results in "$$".</p>
  203. <h2>Nesting of Braces</h2>
  204. <p>In its default configuration Ant will not try to balance braces
  205. in property expansions, it will only consume the text up to the
  206. first closing brace when creating a property name. I.e. when
  207. expanding something like <code>${a${b}}</code> it will be
  208. translated into two parts:</p>
  209. <ol>
  210. <li>the expansion of property <code>a${b</code> - likely nothing
  211. useful.</li>
  212. <li>the literal text <code>}</code> resulting from the second
  213. closing brace</li>
  214. </ol>
  215. <p>This means you can't use easily expand properties whose names are
  216. given by properties, but there
  217. are <a href="http://ant.apache.org/faq.html#propertyvalue-as-name-for-property">some
  218. workarounds</a> for older versions of Ant. With Ant 1.8.0 and the
  219. <a href="http://ant.apache.org/antlib/props/">the props Antlib</a>
  220. you can configure Ant to use
  221. the <code>NestedPropertyExpander</code> defined there if you need
  222. such a feature.</p>
  223. <h2>Expanding a "Property Name"</h2>
  224. <p>In its most simple form <code>${key}</code> is supposed to look
  225. up a property named <code>key</code> and expand to the value of
  226. the property. Additional <code>PropertyEvaluator</code>s may
  227. result in a different interpretation of <code>key</code>,
  228. though.</p>
  229. <p>The <a href="http://ant.apache.org/antlibs/props/">props
  230. Antlib</a> provides a few interesting evaluators but there are
  231. also a few built-in ones.</p>
  232. <a name="toString"><h3>Getting the value of a Reference with
  233. ${toString:}</h3></a>
  234. <p>Any Ant type which has been declared with a reference can also
  235. its string value extracted by using the <code>${toString:}</code>
  236. operation, with the name of the reference listed after
  237. the <code>toString:</code> text. The <code>toString()</code>
  238. method of the Java class instance that is referenced is invoked
  239. -all built in types strive to produce useful and relevant output
  240. in such an instance.</p>
  241. <p>For example, here is how to get a listing of the files in a fileset,<p>
  242. <pre>
  243. &lt;fileset id=&quot;sourcefiles&quot; dir=&quot;src&quot; includes=&quot;**/*.java&quot; /&gt;
  244. &lt;echo&gt; sourcefiles = ${toString:sourcefiles} &lt;/echo&gt;
  245. </pre>
  246. <p>There is no guarantee that external types provide meaningful
  247. information in such a situation</p>
  248. <h3><a name="ant.refid">Getting the value of a Reference with
  249. ${ant.refid:}</a></h3>
  250. <p>Any Ant type which has been declared with a reference can also be
  251. used as a property by using the <code>${ant.refid:}</code>
  252. operation, with the name of the reference listed after
  253. the <code>ant.refid:</code> text. The difference between this
  254. operation and <a href="#toString"><code>${toString:}</code></a> is
  255. that <code>${ant.refid:}</code> will expand to the referenced
  256. object itself. In most circumstances the toString method will be
  257. invoked anyway, for example if the <code>${ant.refid:}</code> is
  258. surrounded by other text.</p>
  259. <p>This syntax is most useful when using a task with attribute
  260. setters that accept objects other than String. For example if the
  261. setter accepts a Resource object as in</p>
  262. <pre>
  263. public void setAttr(Resource r) { ... }
  264. </pre>
  265. <p>then the syntax can be used to pass in resource subclasses
  266. preciously defined as references like</p>
  267. <pre>
  268. &lt;url url="http://ant.apache.org/" id="anturl"/&gt;
  269. &lt;my:task attr="${ant.refid:anturl}"/&gt;
  270. </pre>
  271. </body>