MacroDef
Description
This defines a new task using a
<sequential>
nested task as a template. Nested elements<attribute>
and<element>
are used to specify attributes and elements of the new task. These get substituted into the<sequential>
task when the new task is run.Note
You can also use prior defined attributes for default-values in other attributes. See the examples.
since Apache Ant 1.6
Parameters
Attribute Description Required name The name of the new definition. Yes uri The uri that this definition should live in. No description A description of the macrodef (for documentation purposes only). No backtrace This controls the error traceback if there is an error detected when running the macro. If this is set to true, there will be an error trackback, if false there will not be one. Since Ant 1.7. No; default true Parameters specified as nested elements
attribute
This is used to specify attributes of the new task. The values of the attributes get substituted into the templated task. The attributes will be required attributes unless a default value has been set.
This attribute is placed in the body of the templated task using a notation similar to the ant property notation - @{attribute name}. (May be remembered as "put the substitution AT this location").
The escape sequence @@ is used to escape @. This allows @{x} to be placed in the text without substitution of x by using @@{x}. This corresponds to the $$ escape sequence for properties.
The case of the attribute is ignored, so @{myAttribute} is treated the same as @{MyAttribute}.
Parameters
Attribute Description Required name The name of the new attribute Yes default The default value of the attribute. No description This contains a description of the attribute. since ant 1.6.1 No doubleexpanding Controls whether or not property references in the attribute are expanded twice or just once. See the FAQ for details. since Ant 1.8.3 No; default true element
This is used to specify nested elements of the new task. The contents of the nested elements of the task instance are placed in the templated task at the tag name.
The case of the element name is ignored.
Parameters
Attribute Description Required name The name of the element Yes optional If true this nested element is optional. Default is false - i.e the nested element is required in the new task. No implicit If true this nested element is implicit. This means that any nested elements of the macrodef instance will be placed in the element indicated by the name of this element. There can only be one element if an element is implicit. The default value is false. since ant 1.6.2 No description This contains a description informing the user what the contents of the element are expected to be. since ant 1.6.1 No text
This is used to specify the treatment of text contents of the macro invocation. If this element is not present, then any nested text in the macro invocation will be an error. If the text element is present, then the name becomes an attribute that gets set to the nested text of the macro invocation. Since ant 1.6.1.
The case of the text name is ignored.
Parameters
Attribute Description Required name The name of the text attribute Yes optional If true nested text in the macro is optional, default is "false". No trim If true, the nested text is trimmed of white space, default is "false". No description This contains a description informing the user what the nested text of the macro is expected to be. No Examples
The following example defined a task called testing and runs it.
<macrodef name="testing"> <attribute name="v" default="NOT SET"/> <element name="some-tasks" optional="yes"/> <sequential> <echo>v is @{v}</echo> <some-tasks/> </sequential> </macrodef> <testing v="This is v"> <some-tasks> <echo>this is a test</echo> </some-tasks> </testing>The following fragment defines a task called
<call-cc>
which take the attributes "target", "link" and "target.dir" and the nested element "cc-elements". The body of the task uses the<cc>
task from the ant-contrib project.<macrodef name="call-cc"> <attribute name="target"/> <attribute name="link"/> <attribute name="target.dir"/> <element name="cc-elements"/> <sequential> <mkdir dir="${obj.dir}/@{target}"/> <mkdir dir="@{target.dir}"/> <cc link="@{link}" objdir="${obj.dir}/@{target}" outfile="@{target.dir}/@{target}"> <compiler refid="compiler.options"/> <cc-elements/> </cc> </sequential> </macrodef>This then can be used as follows:
<call-cc target="unittests" link="executable" target.dir="${build.bin.dir}"> <cc-elements> <includepath location="${gen.dir}"/> <includepath location="test"/> <fileset dir="test/unittest" includes = "**/*.cpp"/> <fileset dir="${gen.dir}" includes = "*.cpp"/> <linker refid="linker-libs"/> </cc-elements> </call-cc>The following fragment shows <call-cc>, but this time using an implicit element and with the link and target.dir arguments having default values.
<macrodef name="call-cc"> <attribute name="target"/> <attribute name="link" default="executable"/> <attribute name="target.dir" default="${build.bin.dir}"/> <element name="cc-elements" implicit="yes"/> <sequential> <mkdir dir="${obj.dir}/@{target}"/> <mkdir dir="@{target.dir}"/> <cc link="@{link}" objdir="${obj.dir}/@{target}" outfile="@{target.dir}/@{target}"> <compiler refid="compiler.options"/> <cc-elements/> </cc> </sequential> </macrodef>This then can be used as follows, note that <cc-elements> is not specified.
<call-cc target="unittests"> <includepath location="${gen.dir}"/> <includepath location="test"/> <fileset dir="test/unittest" includes = "**/*.cpp"/> <fileset dir="${gen.dir}" includes = "*.cpp"/> <linker refid="linker-libs"/> </call-cc>The following shows the use of the
text
element.<macrodef name="echotest"> <text name="text"/> <sequential> <echo>@{text}</echo> </sequential> </macrodef> <echotest> Hello world </echotest>The following uses a prior defined attribute for setting the default value of another. The output would be one=test two=test. If you change the order of lines *1 and *2 the output would be one=test two=@{one}, because while processing the two-line the value for one is not set.
<macrodef name="test"> <attribute name="one"/> *1 <attribute name="two" default="@{one}"/> *2 <sequential> <echo>one=@{one} two=@{two}</echo> </sequential> </macrodef> <test one="test"/>