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.

macrodef.html 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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>MacroDef Task</title>
  20. </head>
  21. <body>
  22. <h2 id="macrodef">MacroDef</h2>
  23. <p><em>Since Apache Ant 1.6</em></p>
  24. <h3>Description</h3>
  25. <p>This defines a new task using a <code>&lt;sequential&gt;</code> nested task as a
  26. template. Nested elements <code>&lt;attribute&gt;</code> and <code>&lt;element&gt;</code> are
  27. used to specify attributes and elements of the new task. These get substituted into
  28. the <code>&lt;sequential&gt;</code> task when the new task is run.</p>
  29. <h3>Note</h3>
  30. <p>You can also use <em>prior defined</em> attributes for <var>default</var> values in other
  31. attributes. See the examples.</p>
  32. <h3>Parameters</h3>
  33. <table class="attr">
  34. <tr>
  35. <th>Attribute</th>
  36. <th>Description</th>
  37. <th>Required</th>
  38. </tr>
  39. <tr>
  40. <td>name</td>
  41. <td>The name of the new definition.</td>
  42. <td>Yes</td>
  43. </tr>
  44. <tr>
  45. <td>uri</td>
  46. <td>
  47. The uri that this definition should live in.
  48. </td>
  49. <td>No</td>
  50. </tr>
  51. <tr>
  52. <td>description</td>
  53. <td>A description of the macrodef
  54. (for documentation purposes only).
  55. </td>
  56. <td>No</td>
  57. </tr>
  58. <tr>
  59. <td>backtrace</td>
  60. <td>This controls the error traceback if there is an error detected when running the
  61. macro. If this is set to <q>true</q>, there will be an error trackback, if <q>false</q>
  62. there will be none. <em>Since Ant 1.7</em>.</td>
  63. <td>No; default <q>true</q></td>
  64. </tr>
  65. </table>
  66. <h3>Parameters specified as nested elements</h3>
  67. <h4>attribute</h4>
  68. <p>This is used to specify attributes of the new task. The values of the attributes get
  69. substituted into the templated task. The attributes will be required attributes unless a
  70. default value has been set.</p>
  71. <p>This attribute is placed in the body of the templated task using a notation similar to the
  72. Ant property notation&mdash;<code>@{attribute name}</code>. (May be remembered as "put the
  73. substitution AT this location").</p>
  74. <p>The escape sequence <code>@@</code> is used to escape <code>@</code>. This
  75. allows <code>@{x}</code> to be placed in the text without substitution of <code>x</code> by
  76. using <code>@@{x}</code>. This corresponds to the <code>$$</code> escape sequence for
  77. properties.</p>
  78. <p>The case of the attribute is ignored, so <code>@{myAttribute}</code> is treated the same
  79. as <code>@{MyAttribute}</code>.</p>
  80. <h5>Parameters</h5>
  81. <table class="attr">
  82. <tr>
  83. <th>Attribute</th>
  84. <th>Description</th>
  85. <th>Required</th>
  86. </tr>
  87. <tr>
  88. <td>name</td>
  89. <td>The name of the new attribute</td>
  90. <td>Yes</td>
  91. </tr>
  92. <tr>
  93. <td>default</td>
  94. <td>The default value of the attribute.</td>
  95. <td>No</td>
  96. </tr>
  97. <tr>
  98. <td>description</td>
  99. <td>This contains a description of the attribute. <em>Since Ant 1.6.1</em></td>
  100. <td>No</td>
  101. </tr>
  102. <tr>
  103. <td>doubleexpanding</td>
  104. <td>Controls whether or not property references in the attribute are expanded twice or just
  105. once. See
  106. the <a href="https://ant.apache.org/faq.html#macrodef-property-expansion">FAQ</a> for
  107. details. <em>Since Ant 1.8.3</em></td>
  108. <td>No; default is <q>true</q></td>
  109. </tr>
  110. </table>
  111. <h4>element</h4>
  112. <p>This is used to specify nested elements of the new task. The contents of the nested elements
  113. of the task instance are placed in the templated task at the tag name.</p>
  114. <p>The case of the <code>element</code> name is ignored.</p>
  115. <h5>Parameters</h5>
  116. <table class="attr">
  117. <tr>
  118. <th>Attribute</th>
  119. <th>Description</th>
  120. <th>Required</th>
  121. </tr>
  122. <tr>
  123. <td>name</td>
  124. <td>The name of the element.</td>
  125. <td>Yes</td>
  126. </tr>
  127. <tr>
  128. <td>optional</td>
  129. <td>If <q>true</q> this nested element is optional.</td>
  130. <td>No; default is <q>false</q>&mdash;the nested element is required in the new task</td>
  131. </tr>
  132. <tr>
  133. <td>implicit</td>
  134. <td>If <q>true</q> this nested element is implicit. This means that any nested elements of
  135. the <code>macrodef</code> instance will be placed in the element indicated by the name of
  136. this element. There can only be one element if an element is implicit. <em>Since Ant
  137. 1.6.2</em></td>
  138. <td>No; default is <q>false</q></td>
  139. </tr>
  140. <tr>
  141. <td>description</td>
  142. <td>This contains a description informing the user what the contents of the element are
  143. expected to be. <em>Since Ant 1.6.1</em></td>
  144. <td>No</td>
  145. </tr>
  146. </table>
  147. <h4>text</h4>
  148. <p>This is used to specify the treatment of text contents of the macro invocation. If this
  149. element is not present, then any nested text in the macro invocation will be an error. If
  150. the <code>text</code> element is present, then the name becomes an attribute that gets set to
  151. the nested text of the macro invocation. <em>Since Ant 1.6.1</em>.</p>
  152. <p>The case of the <code>text</code> name is ignored.</p>
  153. <h5>Parameters</h5>
  154. <table class="attr">
  155. <tr>
  156. <th>Attribute</th>
  157. <th>Description</th>
  158. <th>Required</th>
  159. </tr>
  160. <tr>
  161. <td>name</td>
  162. <td>The name of the text attribute.</td>
  163. <td>Yes</td>
  164. </tr>
  165. <tr>
  166. <td>optional</td>
  167. <td>If <q>true</q> nested text in the macro is optional.</td>
  168. <td>No; default is <q>false</q></td>
  169. </tr>
  170. <tr>
  171. <td>trim</td>
  172. <td>If <q>true</q>, the nested text is trimmed of white space.</td>
  173. <td>No; default is <q>false</q></td>
  174. </tr>
  175. <tr>
  176. <td>description</td>
  177. <td>This contains a description informing the user what the nested text of the macro is
  178. expected to be.</td>
  179. <td>No</td>
  180. </tr>
  181. </table>
  182. <h3>Examples</h3>
  183. <p>The following example defined a task called testing and runs it.</p>
  184. <pre class="code">
  185. &lt;macrodef name="testing"&gt;
  186. &lt;attribute name="v" default="NOT SET"/&gt;
  187. &lt;element name="some-tasks" optional="yes"/&gt;
  188. &lt;sequential&gt;
  189. &lt;echo&gt;v is @{v}&lt;/echo&gt;
  190. &lt;some-tasks/&gt;
  191. &lt;/sequential&gt;
  192. &lt;/macrodef&gt;
  193. &lt;testing v="This is v"&gt;
  194. &lt;some-tasks&gt;
  195. &lt;echo&gt;this is a test&lt;/echo&gt;
  196. &lt;/some-tasks&gt;
  197. &lt;/testing&gt;</pre>
  198. <p>The following fragment defines a task called <code>&lt;call-cc&gt;</code> which take the
  199. attributes <var>target</var>, <var>link</var> and <var>target.dir</var> and the nested
  200. element <code>cc-elements</code>. The body of the task uses the <code>&lt;cc&gt;</code> task
  201. from the <a href="http://ant-contrib.sourceforge.net/">ant-contrib</a> project.</p>
  202. <pre class="code">
  203. &lt;macrodef name="call-cc"&gt;
  204. &lt;attribute name="target"/&gt;
  205. &lt;attribute name="link"/&gt;
  206. &lt;attribute name="target.dir"/&gt;
  207. &lt;element name="cc-elements"/&gt;
  208. &lt;sequential&gt;
  209. &lt;mkdir dir="${obj.dir}/@{target}"/&gt;
  210. &lt;mkdir dir="@{target.dir}"/&gt;
  211. &lt;cc link="@{link}" objdir="${obj.dir}/@{target}"
  212. outfile="@{target.dir}/@{target}"&gt;
  213. &lt;compiler refid="compiler.options"/&gt;
  214. &lt;cc-elements/&gt;
  215. &lt;/cc&gt;
  216. &lt;/sequential&gt;
  217. &lt;/macrodef&gt;</pre>
  218. <p>This then can be used as follows:</p>
  219. <pre class="code">
  220. &lt;call-cc target="unittests" link="executable"
  221. target.dir="${build.bin.dir}"&gt;
  222. &lt;cc-elements&gt;
  223. &lt;includepath location="${gen.dir}"/&gt;
  224. &lt;includepath location="test"/&gt;
  225. &lt;fileset dir="test/unittest" includes = "**/*.cpp"/&gt;
  226. &lt;fileset dir="${gen.dir}" includes = "*.cpp"/&gt;
  227. &lt;linker refid="linker-libs"/&gt;
  228. &lt;/cc-elements&gt;
  229. &lt;/call-cc&gt;</pre>
  230. <p>The following fragment shows &lt;call-cc&gt;, but this time using an implicit element and
  231. with the <var>link</var> and <var>target.dir</var> arguments having default values.</p>
  232. <pre class="code">
  233. &lt;macrodef name="call-cc"&gt;
  234. &lt;attribute name="target"/&gt;
  235. &lt;attribute name="link" default="executable"/&gt;
  236. &lt;attribute name="target.dir" default="${build.bin.dir}"/&gt;
  237. &lt;element name="cc-elements" implicit="yes"/&gt;
  238. &lt;sequential&gt;
  239. &lt;mkdir dir="${obj.dir}/@{target}"/&gt;
  240. &lt;mkdir dir="@{target.dir}"/&gt;
  241. &lt;cc link="@{link}" objdir="${obj.dir}/@{target}"
  242. outfile="@{target.dir}/@{target}"&gt;
  243. &lt;compiler refid="compiler.options"/&gt;
  244. &lt;cc-elements/&gt;
  245. &lt;/cc&gt;
  246. &lt;/sequential&gt;
  247. &lt;/macrodef&gt;</pre>
  248. <p>This then can be used as follows, note that &lt;cc-elements&gt; is not specified.</p>
  249. <pre class="code">
  250. &lt;call-cc target="unittests"&gt;
  251. &lt;includepath location="${gen.dir}"/&gt;
  252. &lt;includepath location="test"/&gt;
  253. &lt;fileset dir="test/unittest" includes = "**/*.cpp"/&gt;
  254. &lt;fileset dir="${gen.dir}" includes = "*.cpp"/&gt;
  255. &lt;linker refid="linker-libs"/&gt;
  256. &lt;/call-cc&gt;
  257. </pre>
  258. <p>The following shows the use of the <code>text</code> element.</p>
  259. <pre class="code">
  260. &lt;macrodef name="echotest"&gt;
  261. &lt;text name="text"/&gt;
  262. &lt;sequential&gt;
  263. &lt;echo&gt;@{text}&lt;/echo&gt;
  264. &lt;/sequential&gt;
  265. &lt;/macrodef&gt;
  266. &lt;echotest&gt;
  267. Hello world
  268. &lt;/echotest&gt;
  269. </pre>
  270. <p>The following uses a prior defined attribute for setting the default value of another. The
  271. output would be <code>one=test two=test</code>. If you change the order of lines *1 and *2 the
  272. output would be <code>one=test two=@{one}</code>, because while processing
  273. the <var>two</var>-line the value for <var>one</var> is not set.</p>
  274. <pre class="code">
  275. &lt;macrodef name="test"&gt;
  276. &lt;attribute name="one"/&gt; <strong>*1</strong>
  277. &lt;attribute name="two" default="@{one}"/&gt; <strong>*2</strong>
  278. &lt;sequential&gt;
  279. &lt;echo&gt;one=@{one} two=@{two}&lt;/echo&gt;
  280. &lt;/sequential&gt;
  281. &lt;/macrodef&gt;
  282. &lt;test one="test"/&gt;</pre>
  283. </body>
  284. </html>