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

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