· nant macrodef ant

Macros in nant

One of my favourite features of ant is the ability to create macros where you can define common behaviour and then call it from the rest of your build script.

Unfortunately that task doesn’t come with nant and it’s not available on nant-contrib either.

We were using a very roundabout way to build the various projects in our solution.

12345678910111213141516171819~</td>
  
<target name="compile">    <foreach item="Folder" property="folderName">                  <include name="${project::get-base-directory()}\Project1" />        <include name="${project::get-base-directory()}\Project2" />                          <property name="project.name" value="${path::get-file-name(folderName)}" />      <property name="project.file" value="${project.name}.csproj" />                      <exec program="/path/to/msbuild3.5/">        <arg value="${folderName}\${project.file}" />        <arg value="/p:OutputPath=${build.dir}\${project.name}&quot; />      </exec>                                                  </foreach>        </target>~</td>
</tr></table>

Horrendous! Luckily I happened to be emailing back and forth with Bernardo about Stormwind at the time and he mentioned that there was in fact a task.

I added the Macros dll to the build file and voila:

1234~</td>
  
<target name="compile">  <compile-project projectfile="\path\to\Project1\Project1.csproj" />  <compile-project projectfile="\path\to\Project2\Project2.csproj" /></target>~</td>
</tr></table>


1234567891011121314~</td>
  
<macrodef name="compile-project">            <attribute name="projectfile"/>      <property name="project.name" value="${path::get-file-name-without-extension(projectfile)}" />                    <exec program="${msbuild}">      <arg value="${projectfile}" />      <arg value="/p:OutputPath=${build.dir}\${project.name}&quot; />    </exec>        </macrodef>~</td>
</tr></table>

Further instructions on using the macrodef task are here.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket