My NAnt and NUncle

I was looking for a nice and easy way to automatically build my projects and I didn’t have to look far to find NAnt, a variant of Ant, a build tool for Java projects. If you want to cheat and read ahead, check out http://nant.sourceforge.net but don’t get too comfy, jack, there’s not much out there to guide you along. I’m going to save you some precious time and help provide you with a starter script… because believe me, you’ll be hard pressed to find some good examples on the web. Don’t really expect much out of the examples provided by the good folks down at NAnt, but do dig into their NAnt.build – it contains secrets that you won’t find anywhere.

The first thing you want to do is go to http://nant.sourceforge.net and download the latest version. You’ll need it before you can get your hands dirty. Snag the ‘stable build’ unless you’re a glutton for punishment.

Ok, so you’re ready to spelunk straight into the code, but let me give you a quick overview of NAnt… it’s a tool that automatically builds a .NET project for you. It can also spawn off NUnit, a toolkit that runs your unit tests, and also NDoc, which creates MSDN-like documentation from your commented code.

There’s not much more to the concept of what NAnt does – it allows a developer to create a ‘script’ that compiles a .NET Project and additionally may run your unit tests and create documentation.

Let’s use my fictional company, FrozenHobo, to illustrate the ease in which we can create a build file for NAnt.

First off, we need to create a file called frozenhobo.build. NAnt looks for a build file and executes it – so let’s take a look at the overall picture of a build script and then we’ll dissect it to learn the ins-and-outs of what NAnt is doing behind the scenes.

-==-

Not that bad, is it? As if you couldn’t guess, NAnt is structured in XML, the programmer’s friend… It’s pretty simple to follow and create a build script. So, let me cover a few elements that’ll help you get up to speed on NAnt, and then we’ll get down to business with a quick walk through to illustrate the simplicity of the script.

Properties contain values that can be referenced throughout the build script.
Targets can be seen as a ‘grouping’ of functionality, as well as providing a dependency chain between targets.
References and Sources refer to the assemblies and code that your project uses.

First off, we start out by providing a project tag along with the name and the default action, “build”.

-==-

Now let’s set a few properties that we’ll need, including whether we’ll be making a debug release (pdb), and we’ll also set where our build directory is, in our case, we’ll output our dll to the bin folder.

-==-

Here we’re setting the target name and description, but notice that for our simple example, we’re not using dependencies on other targets. We’ll just compile and output our .dll and be done with it.

-==-

We’re directly calling the c# compiler here, as well as providing it with the proper output for the assembly. In this case, thanks to the properties we’ve already set, we’re going to build our assembly in our bin folder.

-==-

Most projects have some sort of dependency, whether you’ve referenced third-party assemblies or other projects contained in your solution – and NAnt needs to know where those are. Instead of explicitly providing the name of the dependencies, you can prefix the name with a wildcard.

-==-

Hopefully, everyone has advanced to the point that we’re all using code-behinds – here, I’ve explicitly set the source to look directly in the root, but you might have classes located under a classes folder, so you’ll need to include that path as well.

-==-

Alright… You’ve got your script and now I suppose you want to build your project, eh? Since you’ve already got Nant installed, make sure that you’ve included where the Nant executable is on your System Path, and you’re ready to rock. Fire up a command prompt and navigate to where your build script resides. In my case, it’s in:

-==-

once I’m there, I simply execute the command

-==-

and the build script is executed. NAnt will tell you if you’ve screwed up. Believe me.

Using Nant and a build server is a relatively straightforward process that yields powerful results. Get used to creating and deploying build scripts in all of your projects – especially if you’re working on a multi-team environment. You’ll easily spot problems that may lie hidden beneath the surface of your project, especially when you’ve got a developer who likes to code in the ‘dark’. Daily builds can help you keep a finger on the heartbeat of your project – allowing you to circumvent serious problems before then creep up on you.

There is a lot of material to cover when creating a build server that’s beyond the scope of this article. I thought a nice introduction to NAnt and build scripts would jumpstart your interest in learning more about this process. If you’re serious about using NAnt to do your builds, you’ll want to check out the nant-developers archive and/or join the developers list. This will go a long way in helping your learning curve.

Enjoy.

Tiberius OsBurn is a freelance technology author and speaker based in Omaha,
Nebraska. His book, “Hardcore Development”, will be released in the summer
of 2003. Mr. OsBurn has extensive experience in VB, VB.NET, C#, SQL Server,
ASP.NET and various other web technologies. Be sure to visit his site,
http://www.tiberi.us, for his latest articles of interest to .NET developers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top