NAnt, NAntContrib & How can I use msbuild with NAnt?

I had a simple need…

I wanted to check if my NAnt .build file changes were correct before checking them into source control. We already have Cruise Control.NET setup and it works but I did not have any way to test my changes locally. So I thought I would install NAnt and try to test my changes.

So there I went… at first I googled my way to nant.sourceforge.net and found NAnt binaries. Downloaded them, unzipped them and tried to run nant.exe on my build file. Failed. Then I googled my way to discovering that I need something called NAntContrib because it has the definition of the task msbuild that I was using in my .build file. OK so I downloaded NAntContrib. But it wasn’t clear what I should do next. How could I tell nant.exe to load the definition of the msbuild task from NAntContrib binaries?

Anyway, I figured it out (we always do) but had to struggle and google my way around a lot of stuff to get to the point where I was able to run everything.

Here are simple instructions I wish I had. Hopefully they help you out if you’re stuck with the same issue.

What is NAntContrib?

From the NAntContrib readme.txt

“NAntContrib is the project for tasks and tools that haven’t made it into the main NAnt distribution yet or for whatever reason don’t belong there.”

How can I install NAnt and NAnt contrib on my machine?

1. Install NAnt

  • Get the latest Release from here: http://nant.sourceforge.net
  • You can just get the binaries in zip form and unzip on a folder in your machine (say c:\Program Files\nant)

2. Install NAntContrib

  • Get the latest release from here: http://nantcontrib.sourceforge.net
  • Again you can download the binaries in zip form and unzip into a folder in your machine (say c:\Program Files\nantcontrib)

How can I use tasks from NAntContrib in my NAnt?

I was trying to use msbuild which is a task to build a Visual Studio Solution (.sln) file. Here is how I got it to work.

This is how I defined my “build” target in the my .build file

<target name="build" description="Compiles the .Net solution">
      <!-- build the solution -->
      <echo message="Building ${project::get-name()} v${project.version}" />
      <!-- need nant.contrib.tasks.dll for msbuild -->
      <loadtasks assembly="C:/Program Files/nantcontrib/bin/NAnt.Contrib.Tasks.dll" />
      <msbuild project="mysolution.sln">
            <arg value="/p:Configuration=${target}" />
            <arg value="/p:Platform=Any CPU" />
            <arg value="/t:Rebuild" />
      </msbuild>
</target>

By adding the tag in there with the path to the NAnt.Contrib.Tasks.dll, I point nant to the dll to find the msbuild task and it just works.

The command line to run nant on the build file was as follows

c:\Projects\myproject>"c:\program files\nant\bin\nant.exe" -buildfile:mysolution.build build

This tells nant.exe to find mysolution.build in the current folder and execute the “build” target in it.

Advertisements

About floatingfrisbee

A programmer/blogger from New York City
This entry was posted in development tools and tagged , , . Bookmark the permalink.

4 Responses to NAnt, NAntContrib & How can I use msbuild with NAnt?

  1. Stuart says:

    I very much appreciate you documenting this! Thanks very much 🙂

  2. dervalp says:

    Thanks for this useful post, it helps me a lot 🙂

  3. Anonymous says:

    A simpler approach would be to create a folder under your NAnt installation at the path “bin\extensions\common\neutral\NAntContrib” and copy the NAntContrib files there. Then you will not need to add the line to every script.

  4. Anonymous says:

    A simpler approach would be to create a folder under your NAnt installation at the path “bin\extensions\common\neutral\NAntContrib” and copy the NAntContrib files there. Then you will not need to add the loadtasks line to every script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s