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.

local.html 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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>Local Task</title>
  20. </head>
  21. <body>
  22. <h2>Local</h2>
  23. <p><em>Since Ant 1.8</em></p>
  24. <h3>Description</h3>
  25. <p>Adds a local property to the current scope. Property scopes exist at Apache Ant's various "block"
  26. levels. These include targets as well as the <a href="parallel.html">Parallel</a>
  27. and <a href="sequential.html">Sequential</a> task containers
  28. (including <a href="macrodef.html">Macrodef</a> bodies). A local property at a given scope "shadows"
  29. properties of the same name at higher scopes, including the global scope. Note that using
  30. the <code>Local</code> task at the global level effectively makes the property local to the
  31. "anonymous target" in which top-level operations are carried out; it will not be defined for other
  32. targets in the buildfile.</p>
  33. <p>A property is made local if the <code>&lt;local&gt;</code> task precedes its definition. See the
  34. examples section.</p>
  35. <h3>Parameters</h3>
  36. <table class="attr">
  37. <tr>
  38. <th scope="col">Attribute</th>
  39. <th scope="col">Description</th>
  40. <th scope="col">Required</th>
  41. </tr>
  42. <tr>
  43. <td>name</td>
  44. <td>The property to declare in the current scope</td>
  45. <td>Yes</td>
  46. </tr>
  47. </table>
  48. <h3>Examples</h3>
  49. <h4>Temporarily shadow a global property's value</h4>
  50. <pre>
  51. &lt;property name="foo" value="foo"/&gt;
  52. &lt;target name="step1"&gt;
  53. &lt;echo&gt;Before local: foo is ${foo}&lt;/echo&gt;
  54. &lt;local name="foo"/&gt;
  55. &lt;property name="foo" value="bar"/&gt;
  56. &lt;echo&gt;After local: foo is ${foo}&lt;/echo&gt;
  57. &lt;/target&gt;
  58. &lt;target name="step2" depends="step1"&gt;
  59. &lt;echo&gt;In step2: foo is ${foo}&lt;/echo&gt;
  60. &lt;/target&gt;</pre>
  61. <p>outputs</p>
  62. <pre class="output">
  63. step1:
  64. [echo] Before local: foo is foo
  65. [echo] After local: foo is bar
  66. step2:
  67. [echo] In step2: foo is foo</pre>
  68. <p>here the <code>local</code> task shadowed the global definition of <code>foo</code> for the
  69. remainder of the target <q>step1</q>.</p>
  70. <h4>Creating thread local properties</h4>
  71. <pre>
  72. &lt;property name="foo" value="foo"/&gt;
  73. &lt;parallel&gt;
  74. &lt;echo&gt;global 1: foo is ${foo}&lt;/echo&gt;
  75. &lt;sequential&gt;
  76. &lt;local name="foo"/&gt;
  77. &lt;property name="foo" value="bar.1"/&gt;
  78. &lt;echo&gt;First sequential: foo is ${foo}&lt;/echo&gt;
  79. &lt;/sequential&gt;
  80. &lt;sequential&gt;
  81. &lt;sleep seconds="1"/&gt;
  82. &lt;echo&gt;global 2: foo is ${foo}&lt;/echo&gt;
  83. &lt;/sequential&gt;
  84. &lt;sequential&gt;
  85. &lt;local name="foo"/&gt;
  86. &lt;property name="foo" value="bar.2"/&gt;
  87. &lt;echo&gt;Second sequential: foo is ${foo}&lt;/echo&gt;
  88. &lt;/sequential&gt;
  89. &lt;echo&gt;global 3: foo is ${foo}&lt;/echo&gt;
  90. &lt;/parallel&gt;</pre>
  91. <p>outputs something similar to</p>
  92. <pre class="output">
  93. [echo] global 3: foo is foo
  94. [echo] global 1: foo is foo
  95. [echo] First sequential: foo is bar.1
  96. [echo] Second sequential: foo is bar.2
  97. [echo] global 2: foo is foo</pre>
  98. <h4>Use inside <code>macrodef</code></h4>
  99. <p>This probably is where <code>local</code> can be applied in the most useful way. If you needed a
  100. "temporary property" inside a <code>macrodef</code> in Ant prior to Ant 1.8.0 you had to try to come
  101. up with a property name that would be unique across macro invocations.</p>
  102. <p>Say you wanted to write a macro that created the parent directory of a given file. A naive
  103. approach would be:</p>
  104. <pre>
  105. &lt;macrodef name="makeparentdir"&gt;
  106. &lt;attribute name="file"/&gt;
  107. &lt;sequential&gt;
  108. &lt;dirname property="parent" file="@{file}"/&gt;
  109. &lt;mkdir dir="${parent}"/&gt;
  110. &lt;/sequential&gt;
  111. &lt;/macrodef&gt;
  112. &lt;makeparentdir file="some-dir/some-file"/&gt;</pre>
  113. <p>but this would create a global property <code>parent</code> on the first invocation&mdash;and
  114. since properties are not mutable, any subsequent invocation will see the same value and try to
  115. create the same directory as the first invocation.</p>
  116. <p>The recommendation prior to Ant 1.8.0 was to use a property name based on one of the macro's
  117. attributes, like</p>
  118. <pre>
  119. &lt;macrodef name="makeparentdir"&gt;
  120. &lt;attribute name="file"/&gt;
  121. &lt;sequential&gt;
  122. &lt;dirname property="parent.@{file}" file="@{file}"/&gt;
  123. &lt;mkdir dir="${parent.@{file}}"/&gt;
  124. &lt;/sequential&gt;
  125. &lt;/macrodef&gt;</pre>
  126. <p>Now invocations for different files will set different properties and the directories will get
  127. created. Unfortunately this "pollutes" the global properties space. In addition, it may be hard to
  128. come up with unique names in some cases.</p>
  129. <p>Enter <code>&lt;local&gt;</code>:</p>
  130. <pre>
  131. &lt;macrodef name="makeparentdir"&gt;
  132. &lt;attribute name="file"/&gt;
  133. &lt;sequential&gt;
  134. &lt;local name="parent"/&gt;
  135. &lt;dirname property="parent" file="@{file}"/&gt;
  136. &lt;mkdir dir="${parent}"/&gt;
  137. &lt;/sequential&gt;
  138. &lt;/macrodef&gt;</pre>
  139. <p>Each invocation gets its own property named <code>parent</code> and there will be no global
  140. property of that name at all.</p>
  141. </body>
  142. </html>