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

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