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

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