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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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"></meta>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>MacroDef Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="macrodef">MacroDef</a></h2>
  23. <h3>Description</h3>
  24. <p>
  25. This defines a new task using a <code>&lt;sequential&gt;</code>
  26. nested task as a template. Nested elements <code>&lt;attribute&gt;</code> and
  27. <code>&lt;element&gt;</code> are used to specify attributes and elements of
  28. the new task. These get substituted into the <code>&lt;sequential&gt;</code>
  29. task when the new task is run.
  30. </p>
  31. <h3>Note</h3>
  32. <p>
  33. You can also use <i>prior defined</i> attributes for default-values in
  34. other attributes. See the examples.
  35. </p>
  36. <p>
  37. <em>since Ant 1.6</em>
  38. </p>
  39. <h3>Parameters</h3>
  40. <table border="1" cellpadding="2" cellspacing="0">
  41. <tr>
  42. <td valign="top"><b>Attribute</b></td>
  43. <td valign="top"><b>Description</b></td>
  44. <td align="center" valign="top"><b>Required</b></td>
  45. </tr>
  46. <tr>
  47. <td valign="top">name</td>
  48. <td valign="top">The name of the new definition.</td>
  49. <td valign="top" align="center">Yes</td>
  50. </tr>
  51. <tr>
  52. <td valign="top">uri</td>
  53. <td valign="top">
  54. The uri that this definition should live in.
  55. </td>
  56. <td valign="top" align="center">No</td>
  57. </tr>
  58. <tr>
  59. <td valign="top">description</td>
  60. <td valign="top">A description of the macrodef
  61. (for documentation purposes only).
  62. </td>
  63. <td valign="top" align="center">No</td>
  64. </tr>
  65. <tr>
  66. <td valign="top">backtrace</td>
  67. <td valign="top">
  68. This controls the error traceback if they is an
  69. error detected when running the macro. If this is
  70. set to true, there will be an error trackback, if false
  71. there will not be one. <em>Since Ant 1.7</em>.
  72. </td>
  73. <td valign="top" align="center">No; default <em>true</em></td>
  74. </tr>
  75. </table>
  76. <h3>Parameters specified as nested elements</h3>
  77. <h4>attribute</h4>
  78. <p>
  79. This is used to specify attributes of the new task. The values
  80. of the attributes get substituted into the templated task.
  81. The attributes will be required attributes unless a default
  82. value has been set.
  83. </p>
  84. <p>
  85. This attribute is placed in the body of the templated
  86. task using a notation similar to the ant property notation
  87. - @{attribute name}. (May be remembered as "put the substitution
  88. AT this location").
  89. </p>
  90. <p>
  91. The escape sequence @@ is used to escape @. This allows @{x} to be
  92. placed in the text without substitution of x by using @@{x}.
  93. This corresponds to the $$ escape sequence for properties.
  94. </p>
  95. <p>
  96. The case of the attribute is ignored, so @{myAttribute} is treated the
  97. same as @{MyAttribute}.
  98. </p>
  99. <h3>Parameters</h3>
  100. <table border="1" cellpadding="2" cellspacing="0">
  101. <tr>
  102. <td valign="top"><b>Attribute</b></td>
  103. <td valign="top"><b>Description</b></td>
  104. <td align="center" valign="top"><b>Required</b></td>
  105. </tr>
  106. <tr>
  107. <td valign="top">name</td>
  108. <td valign="top">The name of the new attribute</td>
  109. <td valign="top" align="center">Yes</td>
  110. </tr>
  111. <tr>
  112. <td valign="top">default</td>
  113. <td valign="top">
  114. The default value of the attribute.
  115. </td>
  116. <td valign="top" align="center">No</td>
  117. </tr>
  118. <tr>
  119. <td valign="top">description</td>
  120. <td valign="top">
  121. This contains a description of the attribute.
  122. <em>since ant 1.6.1</em>
  123. </td>
  124. <td valign="top" align="center">No</td>
  125. </tr>
  126. </table>
  127. <h4>element</h4>
  128. <p>
  129. This is used to specify nested elements of the new task.
  130. The contents of the nested elements of the task instance
  131. are placed in the templated task at the tag name.
  132. </p>
  133. <p>
  134. The case of the element name is ignored.
  135. </p>
  136. <h3>Parameters</h3>
  137. <table border="1" cellpadding="2" cellspacing="0">
  138. <tr>
  139. <td valign="top"><b>Attribute</b></td>
  140. <td valign="top"><b>Description</b></td>
  141. <td align="center" valign="top"><b>Required</b></td>
  142. </tr>
  143. <tr>
  144. <td valign="top">name</td>
  145. <td valign="top">The name of the element</td>
  146. <td valign="top" align="center">Yes</td>
  147. </tr>
  148. <tr>
  149. <td valign="top">optional</td>
  150. <td valign="top">
  151. If true this nested element is optional. Default is
  152. false - i.e the nested element is required in
  153. the new task.
  154. </td>
  155. <td valign="top" align="center">No</td>
  156. </tr>
  157. <tr>
  158. <td valign="top">implicit</td>
  159. <td valign="top">
  160. If true this nested element is implicit. This means that
  161. any nested elements of the macrodef instance will be placed
  162. in the element indicated by the name of this element.
  163. There can only be one element if an element is implicit.
  164. The default value is false. <em>since ant 1.6.2</em>
  165. </td>
  166. <td valign="top" align="center">No</td>
  167. </tr>
  168. <tr>
  169. <td valign="top">description</td>
  170. <td valign="top">
  171. This contains a description
  172. informing the user what the contents of the element are expected to be.
  173. <em>since ant 1.6.1</em>
  174. </td>
  175. <td valign="top" align="center">No</td>
  176. </tr>
  177. </table>
  178. <h4>text</h4>
  179. <p>
  180. This is used to specify the treatment of text contents of the macrodef.
  181. If this element is not present, then any nested text in the macro
  182. will be an error. If the text element is present, then the name
  183. becomes an attribute that gets set to the nested text of the macro.
  184. <em>Since ant 1.6.1.</em>
  185. </p>
  186. <p>
  187. The case of the text name is ignored.
  188. </p>
  189. <h3>Parameters</h3>
  190. <table border="1" cellpadding="2" cellspacing="0">
  191. <tr>
  192. <td valign="top"><b>Attribute</b></td>
  193. <td valign="top"><b>Description</b></td>
  194. <td align="center" valign="top"><b>Required</b></td>
  195. </tr>
  196. <tr>
  197. <td valign="top">name</td>
  198. <td valign="top">The name of the text attribute</td>
  199. <td valign="top" align="center">Yes</td>
  200. </tr>
  201. <tr>
  202. <td valign="top">optional</td>
  203. <td valign="top">
  204. If true nested text in the macro is optional, default is "false".
  205. </td>
  206. <td valign="top" align="center">No</td>
  207. </tr>
  208. <tr>
  209. <td valign="top">trim</td>
  210. <td valign="top">
  211. If true, the nested text is trimmed of white space,
  212. default is "false".
  213. </td>
  214. <td valign="top" align="center">No</td>
  215. </tr>
  216. <tr>
  217. <td valign="top">description</td>
  218. <td valign="top">
  219. This contains a description
  220. informing the user what the nested text of the macro is expected
  221. to be.
  222. </td>
  223. <td valign="top" align="center">No</td>
  224. </tr>
  225. </table>
  226. <h3>Examples</h3>
  227. <p>
  228. The following example defined a task called testing and
  229. runs it.
  230. </p>
  231. <blockquote>
  232. <pre class=code>
  233. &lt;macrodef name="testing"&gt;
  234. &lt;attribute name="v" default="NOT SET"/&gt;
  235. &lt;element name="some-tasks" optional="yes"/&gt;
  236. &lt;sequential&gt;
  237. &lt;echo&gt;v is @{v}&lt;/echo&gt;
  238. &lt;some-tasks/&gt;
  239. &lt;/sequential&gt;
  240. &lt;/macrodef&gt;
  241. &lt;testing v="This is v"&gt;
  242. &lt;some-tasks&gt;
  243. &lt;echo&gt;this is a test&lt;/echo&gt;
  244. &lt;/some-tasks&gt;
  245. &lt;/testing&gt;
  246. </pre>
  247. </blockquote>
  248. <p>
  249. The following fragment defines a task called <code>&lt;call-cc&gt;</code> which
  250. take the attributes "target", "link" and "target.dir" and the
  251. nested element "cc-elements". The body of the task
  252. uses the <code>&lt;cc&gt;</code> task from the
  253. <a href="http://ant-contrib.sourceforge.net/">ant-contrib</a> project.
  254. </p>
  255. <blockquote>
  256. <pre class="code">
  257. &lt;macrodef name="call-cc"&gt;
  258. &lt;attribute name="target"/&gt;
  259. &lt;attribute name="link"/&gt;
  260. &lt;attribute name="target.dir"/&gt;
  261. &lt;element name="cc-elements"/&gt;
  262. &lt;sequential&gt;
  263. &lt;mkdir dir="${obj.dir}/@{target}"/&gt;
  264. &lt;mkdir dir="@{target.dir}"/&gt;
  265. &lt;cc link="@{link}" objdir="${obj.dir}/@{target}"
  266. outfile="@{target.dir}/@{target}"&gt;
  267. &lt;compiler refid="compiler.options"/&gt;
  268. &lt;cc-elements/&gt;
  269. &lt;/cc&gt;
  270. &lt;/sequential&gt;
  271. &lt;/macrodef&gt;
  272. </pre>
  273. </blockquote>
  274. <p>
  275. This then can be used as follows:
  276. </p>
  277. <blockquote>
  278. <pre class="code">
  279. &lt;call-cc target="unittests" link="executable"
  280. target.dir="${build.bin.dir}"&gt;
  281. &lt;cc-elements&gt;
  282. &lt;includepath location="${gen.dir}"/&gt;
  283. &lt;includepath location="test"/&gt;
  284. &lt;fileset dir="test/unittest" includes = "**/*.cpp"/&gt;
  285. &lt;fileset dir="${gen.dir}" includes = "*.cpp"/&gt;
  286. &lt;linker refid="linker-libs"/&gt;
  287. &lt;/cc-elements&gt;
  288. &lt;/call-cc&gt;
  289. </pre>
  290. </blockquote>
  291. <p>
  292. The following fragment shows &lt;call-cc&gt;, but this time
  293. using an implicit element and with the link and target.dir arguments
  294. having default values.
  295. </p>
  296. <blockquote>
  297. <pre class="code">
  298. &lt;macrodef name="call-cc"&gt;
  299. &lt;attribute name="target"/&gt;
  300. &lt;attribute name="link" default="executable"/&gt;
  301. &lt;attribute name="target.dir" default="${build.bin.dir}"/&gt;
  302. &lt;element name="cc-elements" implicit="yes"/&gt;
  303. &lt;sequential&gt;
  304. &lt;mkdir dir="${obj.dir}/@{target}"/&gt;
  305. &lt;mkdir dir="@{target.dir}"/&gt;
  306. &lt;cc link="@{link}" objdir="${obj.dir}/@{target}"
  307. outfile="@{target.dir}/@{target}"&gt;
  308. &lt;compiler refid="compiler.options"/&gt;
  309. &lt;cc-elements/&gt;
  310. &lt;/cc&gt;
  311. &lt;/sequential&gt;
  312. &lt;/macrodef&gt;
  313. </pre>
  314. </blockquote>
  315. <p>
  316. This then can be used as follows, note that &lt;cc-elements&gt;
  317. is not specified.
  318. </p>
  319. <blockquote>
  320. <pre class="code">
  321. &lt;call-cc target="unittests"/&gt;
  322. &lt;includepath location="${gen.dir}"/&gt;
  323. &lt;includepath location="test"/&gt;
  324. &lt;fileset dir="test/unittest" includes = "**/*.cpp"/&gt;
  325. &lt;fileset dir="${gen.dir}" includes = "*.cpp"/&gt;
  326. &lt;linker refid="linker-libs"/&gt;
  327. &lt;/call-cc&gt;
  328. </pre>
  329. </blockquote>
  330. <p>
  331. The following shows the use of the <code>text</code> element.
  332. </p>
  333. <blockquote>
  334. <pre class="code">
  335. &lt;macrodef name="echotest"&gt;
  336. &lt;text name="text"/&gt;
  337. &lt;sequential&gt;
  338. &lt;echo&gt;@{text}&lt;/echo&gt;
  339. &lt;/sequential&gt;
  340. &lt;/macrodef&gt;
  341. &lt;echotest&gt;
  342. Hello world
  343. &lt;/echotest&gt;
  344. </pre>
  345. </blockquote>
  346. <p>
  347. The following uses a prior defined attribute for setting the
  348. default value of another. The output would be
  349. <tt>one=test two=test</tt>. If you change the order of lines
  350. *1 and *2 the output would be <tt>one=test two=@{one}</tt>,
  351. because while processing the <i>two</i>-line the value for
  352. <i>one</i> is not set.
  353. </p>
  354. <blockquote>
  355. <pre class="code">
  356. &lt;macrodef name="test"&gt;
  357. &lt;attribute name="one"/&gt; <b>*1</b>
  358. &lt;attribute name="two" default="@{one}"/&gt; <b>*2</b>
  359. &lt;sequential&gt;
  360. &lt;echo&gt;one=@{one} two=@{two}&lt;/echo&gt;
  361. &lt;/sequential&gt;
  362. &lt;/macrodef&gt;
  363. &lt;test one="test"/&gt;
  364. </pre>
  365. </blockquote>
  366. </body>
  367. </html>