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

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