Peel me a Grape :: We make things work

NAnt Macrodef (.Net)

Macrodef task for NAnt

NAnt by default doesn’t have support for ant style macrodef, as discussed in this mailing list post .

While working for Exoftware, I was working with some clients using .net. I knocked together this custom task for NAnt to give me macrodefs. Syntax is similar to in ant, and attributes and elements are supported. Usage is like this:
<loadtasks assembly="tools/Macrodef.dll" />
To define a macro:
<macrodef name="assert-equals">
        <attributes>
                <attribute name="name"/>
                <attribute name="expected"/>
                <attribute name="actual"/>
        </attributes>
        <sequential>
                <fail if="${ expected != actual}" message="${name}: expected '${expected}' but was '${actual}'"/>
        </sequential>
</macrodef>
and calling it:
<assert-equals name="a" expected="a" actual="${actual_a}"/>
(examples taken from test build file)

Implementation Notes

The implementation uses a bit of a hack to register the new task name with NAnt – it compiles some dynamically generated code and registers the resulting assembly. So when using the task, you will see messages like:
[macrodef] Scanning assembly "66f4d2c6" for extensions.
It would be easy enough to change NAnt to avoid this step, and probably speed up the runtime also. I may look into developing this as a patch to NAnt.

Download:

  • Can download the source code from