MacroDef

Description

This defines a new task using a <sequential> or <parallel> 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> or <parallel> task when the new task is run.

since 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

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 the ant property notation - ${attribute name}. Note that is not an actual ant property.

Parameters

Attribute Description Required
name The name of the new attribute Yes
default The default value of the attribute. No

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.

Parameters

Attribute Description Required
name The name of the new attribute Yes
optional If true this nested element is optional. Default is false - i.e the nested element is required in the new task. 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>
      

Copyright © 2003 Apache Software Foundation. All rights Reserved.