Basic build script with NAnt

14.09.2009 15:56Comments
In this post I will show you how to create a build script for a VS solution using NAnt. NAnt is a free .NET build tool inspired in the JAVA version ANT. Here is the link to the website, where you can get the latest stable version. First things first, why would I want a script to build my projects, when I could just hit F5 from visual studio? In a very simple project, you may not need to create a script, but on bigger projects, it comes very handy when automating tasks such as package generation, assembly versioning, and even deployment! Apart from that which is not little, when used with a continuous integration tool (such as cruise control .net), you can build, run tests and even calculate the coverage of your project after each commit. If you used the structure I recommended in my previous post about structuring your source code folders (or similar), then you must have a "lib" folder right next to your "src" folder. This is where I unzipped the Nant's content, inside a folder called "NAnt 0.86". There are three different ways to build your solution from an NAnt script, and these are:
  • Running a shell comand through the "exec" NAnt task.
  • By adding a "solution" task, which is a task provided by the NAnt that compiles VS solutions. Unfortunately this task doesn't support the newer versions of Visual Studio such as 2008. When using this task, you may get  two errors: "Failed to initialize the 'Microsoft .NET Framework 2.0'" which you can solve downloading the latest version from; and "Solution format of file 'd:SVN.......sln' is not supported" in which case you are doomed, and should go with the next option.
  • Using the NAnt contrib's task "msbuild". This is the one I will be using in this post.
Download the NAnt contrib library from, and extract the content to the folder next to NAnt 0.86 inside the lib folder. So basically our folders should look like this: nant So now we are going to create the build script inside our trunk folder. NAnt scripts are xml documents, usually with a build extension. I will call my file, and it looks like this: basic nant build file Let's go through the file tag by tag.
  • Project. This is the root element of the build file. Notice the default attribute is set to build. This means that when you run NAnt without any target parameters, it will use the "build" target as default.
  • Property. This are properties you can use later in the configuration. Using properties instead of typing the code in the script will help you when modifying the script, so you don't have to search & replace the whole file. Notice how the "lib.nant.contrib" property uses the "root.dir" property through ${root.dir}. That is how you will reference your properties throughout the rest of your script.
  • Target. This is what gets executed when calling NAnt. Here you can have a series of tasks that will be executed. Our first task is a "loadtasks" task, that given a certain dll searches for NAnt tasks. In this case, we are passing the NAnt.Contrib.Tasks.dll, which contains the "msbuild" tasks (amongst others).
  • msbuild. This is the NAnt contrib's task that will call msbuild to build our solution. See how we can set the Configuration and Platform properties.
Running the script In order to run the script we need to execute the nant.exe program which resides in the lib folder. In order to do this, I usually create a batch file with the call to this program. So in our case, I would create a build.bat file inside the "trunk" folder and place the following: ".libnAnt 0.86nant.exe". Notice that we are running nant.exe from the trunk, so nant.exe will search for *.build files and execute the first. Because we are not passing any arguments, the default target "build" is called. In order to specify the build file we use the /f argument, and we also specify the target we want to execute: ".libnAnt 0.86nant.exe / build". You can add a line to the batch file to be able to see the output with a "pause" command. Now that you know how to create and run NAnt scripts, it would be nice to have intellisense in Visual Studio. So you can take a quick look at this post on how achieve this. One quick note on this is to make sure that you have the right schema name in your build file.  The xmlns value of the project tag in your build file, should match the xmlns:nant field of the nant.xsd. Conclusion We have added both NAnt and NAnt Contrib to a place where the entire team has access, so now everyone on the team will be using the same versions. Then we created a simple build file which builds our solution, and provided a batch file for simplicity.

comments powered by Disqus