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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 the <a href="https://ant.apache.org/faq.html#macrodef-property-expansion"
  106. target="_top">FAQ</a> for details. <em>Since Ant 1.8.3</em></td>
  107. <td>No; default is <q>true</q></td>
  108. </tr>
  109. </table>
  110. <h4>element</h4>
  111. <p>This is used to specify nested elements of the new task. The contents of the nested elements
  112. of the task instance are placed in the templated task at the tag name.</p>
  113. <p>The case of the <code>element</code> name is ignored.</p>
  114. <h5>Parameters</h5>
  115. <table class="attr">
  116. <tr>
  117. <th>Attribute</th>
  118. <th>Description</th>
  119. <th>Required</th>
  120. </tr>
  121. <tr>
  122. <td>name</td>
  123. <td>The name of the element.</td>
  124. <td>Yes</td>
  125. </tr>
  126. <tr>
  127. <td>optional</td>
  128. <td>If <q>true</q> this nested element is optional.</td>
  129. <td>No; default is <q>false</q>&mdash;the nested element is required in the new task</td>
  130. </tr>
  131. <tr>
  132. <td>implicit</td>
  133. <td>If <q>true</q> this nested element is implicit. This means that any nested elements of
  134. the <code>macrodef</code> instance will be placed in the element indicated by the name of
  135. this element. There can only be one element if an element is implicit. <em>Since Ant
  136. 1.6.2</em></td>
  137. <td>No; default is <q>false</q></td>
  138. </tr>
  139. <tr>
  140. <td>description</td>
  141. <td>This contains a description informing the user what the contents of the element are
  142. expected to be. <em>Since Ant 1.6.1</em></td>
  143. <td>No</td>
  144. </tr>
  145. </table>
  146. <h4>text</h4>
  147. <p>This is used to specify the treatment of text contents of the macro invocation. If this
  148. element is not present, then any nested text in the macro invocation will be an error. If
  149. the <code>text</code> element is present, then the name becomes an attribute that gets set to
  150. the nested text of the macro invocation. <em>Since Ant 1.6.1</em>.</p>
  151. <p>The case of the <code>text</code> name is ignored.</p>
  152. <h5>Parameters</h5>
  153. <table class="attr">
  154. <tr>
  155. <th>Attribute</th>
  156. <th>Description</th>
  157. <th>Required</th>
  158. </tr>
  159. <tr>
  160. <td>name</td>
  161. <td>The name of the text attribute.</td>
  162. <td>Yes</td>
  163. </tr>
  164. <tr>
  165. <td>optional</td>
  166. <td>If <q>true</q> nested text in the macro is optional.</td>
  167. <td>No; default is <q>false</q></td>
  168. </tr>
  169. <tr>
  170. <td>trim</td>
  171. <td>If <q>true</q>, the nested text is trimmed of white space.</td>
  172. <td>No; default is <q>false</q></td>
  173. </tr>
  174. <tr>
  175. <td>description</td>
  176. <td>This contains a description informing the user what the nested text of the macro is
  177. expected to be.</td>
  178. <td>No</td>
  179. </tr>
  180. </table>
  181. <h3>Examples</h3>
  182. <p>The following example defined a task called testing and runs it.</p>
  183. <pre>
  184. &lt;macrodef name="testing"&gt;
  185. &lt;attribute name="v" default="NOT SET"/&gt;
  186. &lt;element name="some-tasks" optional="yes"/&gt;
  187. &lt;sequential&gt;
  188. &lt;echo&gt;v is @{v}&lt;/echo&gt;
  189. &lt;some-tasks/&gt;
  190. &lt;/sequential&gt;
  191. &lt;/macrodef&gt;
  192. &lt;testing v="This is v"&gt;
  193. &lt;some-tasks&gt;
  194. &lt;echo&gt;this is a test&lt;/echo&gt;
  195. &lt;/some-tasks&gt;
  196. &lt;/testing&gt;</pre>
  197. <p>The following fragment defines a task called <code>&lt;call-cc&gt;</code> which take the
  198. attributes <var>target</var>, <var>link</var> and <var>target.dir</var> and the nested
  199. element <code>cc-elements</code>. The body of the task uses the <code>&lt;cc&gt;</code> task
  200. from the <a href="http://ant-contrib.sourceforge.net/" target="_top">ant-contrib</a>
  201. project.</p>
  202. <pre>
  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>
  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 <code>&lt;call-cc&gt;</code>, but this time using an implicit
  231. element and with the <var>link</var> and <var>target.dir</var> arguments having default
  232. values.</p>
  233. <pre>
  234. &lt;macrodef name="call-cc"&gt;
  235. &lt;attribute name="target"/&gt;
  236. &lt;attribute name="link" default="executable"/&gt;
  237. &lt;attribute name="target.dir" default="${build.bin.dir}"/&gt;
  238. &lt;element name="cc-elements" implicit="yes"/&gt;
  239. &lt;sequential&gt;
  240. &lt;mkdir dir="${obj.dir}/@{target}"/&gt;
  241. &lt;mkdir dir="@{target.dir}"/&gt;
  242. &lt;cc link="@{link}" objdir="${obj.dir}/@{target}"
  243. outfile="@{target.dir}/@{target}"&gt;
  244. &lt;compiler refid="compiler.options"/&gt;
  245. &lt;cc-elements/&gt;
  246. &lt;/cc&gt;
  247. &lt;/sequential&gt;
  248. &lt;/macrodef&gt;</pre>
  249. <p>This then can be used as follows, note that <code>&lt;cc-elements&gt;</code> is not
  250. specified.</p>
  251. <pre>
  252. &lt;call-cc target="unittests"&gt;
  253. &lt;includepath location="${gen.dir}"/&gt;
  254. &lt;includepath location="test"/&gt;
  255. &lt;fileset dir="test/unittest" includes = "**/*.cpp"/&gt;
  256. &lt;fileset dir="${gen.dir}" includes = "*.cpp"/&gt;
  257. &lt;linker refid="linker-libs"/&gt;
  258. &lt;/call-cc&gt;
  259. </pre>
  260. <p>The following shows the use of the <code>text</code> element.</p>
  261. <pre>
  262. &lt;macrodef name="echotest"&gt;
  263. &lt;text name="text"/&gt;
  264. &lt;sequential&gt;
  265. &lt;echo&gt;@{text}&lt;/echo&gt;
  266. &lt;/sequential&gt;
  267. &lt;/macrodef&gt;
  268. &lt;echotest&gt;
  269. Hello world
  270. &lt;/echotest&gt;
  271. </pre>
  272. <p>The following uses a prior defined attribute for setting the default value of another. The
  273. output would be <code class="output">one=test two=test</code>. If you change the order of
  274. lines *1 and *2 the output would be <code class="output">one=test two=@{one}</code>, because
  275. while processing the <var>two</var>-line the value for <var>one</var> is not set.</p>
  276. <pre>
  277. &lt;macrodef name="test"&gt;
  278. &lt;attribute name="one"/&gt; <strong>*1</strong>
  279. &lt;attribute name="two" default="@{one}"/&gt; <strong>*2</strong>
  280. &lt;sequential&gt;
  281. &lt;echo&gt;one=@{one} two=@{two}&lt;/echo&gt;
  282. &lt;/sequential&gt;
  283. &lt;/macrodef&gt;
  284. &lt;test one="test"/&gt;</pre>
  285. </body>
  286. </html>