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

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