Simple NAnt integration with Visual Studio

Generally I use the most excellent TestDriven.NET for my building in Visual Studio as it can run tests and identify failed tests (with a context to jump to the error). Often though I have a setup (usually with a client) where they don't have the ability to run this addin or just want to build their systems and don't have unit tests (yes, it does happen, oh horrors of horrors). Also TestDriven.NET won't build an entire system and do additional stuff like maybe copying files for deployment, running database scripts for agile database development, etc. so you look to something more complete to automate the process.

The key thing about building systems when you have multiple team members is to have everyone build the same thing. We want to avoid the "it works on my machine" syndrome. This can be a problem though when someone goes off and tweaks some personal setting, or only builds part of the solution and forgets steps if the build is complicated. For the most part it's as simple as Ctrl+Shift+B, but sometimes that's not enough.

That's where NAnt comes in. JP Boodhoo has an excellent series of blog posts on automating your builds with NAnt in his NAnt Starter Series (I highly recommend JP's blog in any case as it just plain rocks) but I wanted  to pass on a quick tip for those of us that want to get our feet wet, but don't want to leave the comfort of the Ctrl+Shift+B world.

If you've followed his series or have your own simple build file that you want everyone to use here's a super-simple way to make it your default build tool:

  1. Create a new External Tool by going to Tools | External Tools
  2. Click Add to add a new tool
  3. Give it a title of "NAnt"
  4. Browse to the location of the NAnt.exe file wherever you have it downloaded to
  5. Set the initial directory to $(SolutionDir) (where your .build file resides)
  6. Click on "Use Output Window"
  7. Click OK
  8. In the external tools menu, select NAnt and move it up to the top so it's the first tool (we'll see why later)

Now override the default keybinding of Ctrl+Shift+B to run this. Here's how:

  1. Go to the Options dialog by going to Tools | Options
  2. Under the Environment node in the tree click on Keyboard
  3. Find the command called "Tools.ExternalCommand1" by entering it the "Show commands containing:" text box or scrolling through the list
  4. Click on the "Press shortcut key(s):" text box
  5. Press Ctrl+Shift+B
  6. You'll see it's already assigned to the "Build.BuildSolution" command but click on the "Assign" button to reassign it
  7. Click OK to close the dialog.

Now, armed with your .build file in your solution directory press Ctrl+Shift+B and it will open up the Output window, run NAnt (which will run the .build file automagically) and output the results to your window. Grant you, if the build fails it won't highlight the error and let you click on it to go to the file but at least the build will run automatically and everyone will be on the same page.

If you have any additional steps that would be done as a result of the build (running unit tests, copying files, etc.) you can just edit the .build file (hint: make it a solution item and put it under source control as it's no different than code) and rebuild. All the steps will happen and everyone in the team will be running the same sequence of events when they build the solution.

These instructions are for VS2003 but VS2005 works the same.

1 Comment

  • Sounds like a great idea. I'm not convinced I want to bet the farm on MSBuild yet, so it's good to know there's some basic support for NAnt build files too. Any thoughts about how to pass in the name of the target you want to run?

Comments have been disabled for this content.