Click here to Skip to main content
13,348,502 members (63,471 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 10 Aug 2007

Introduction to NAnt

, 10 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Writing NAnt scripts for NDoc, FxCop,NUnit. and NCoverage.


Nant is the scripting tool for the Dot Net platform. It is inspired from its java counterpart, <st1:place w:st="on">Ant. <o:p />

NAnt is an open source project, with a homepage at There<o:p />

are links to many resources from this page. Additionally, there is an adjunct to NAnt called NAntContrib. It has a homepage at This project is for task contributions to NAnt that do<o:p />

not make it into the core NAnt release. Occasionally, tasks in NAntContrib will make the<o:p />

switch to NAnt.<o:p />

NAnt Nomenclature<o:p />

Just before we dive in, let us clarify some of the terminology that we will use when discussing<o:p />

NAnt:<o:p />

NAnt: The physical executable (and the associated tasks) that form the application.<o:p />

NAnt script/build script/build file: An XML file describing a project comprised of one or<o:p />

more targets and zero or more properties.<o:p />

Project: The root node of a build script. Each script has one project.<o:p />

Target: A subdivision of a project. A target is a discrete piece of work that can consist of<o:p />

zero or more tasks. Targets can be dependent on each other.<o:p />

Task: A task is a defined activity that NAnt will execute; for example, creating a directory<o:p />

or zipping a file. There are many built-in tasks to NAnt, and we will look at the most valuable<o:p />

of these in Chapter 3.<o:p />

Property: An XML key/value pair that can be used to configure tasks and targets in a<o:p />

more dynamic fashion. Property values can be overridden from the command line.

Installation Instructions:<o:p />

<o:p> Download the Nant executable from the Nant site, The latest version was 0.85 at the time of writing this article. Extract the executable to a local directory, say C:\Nant\. The binary executable for nant will be stored in C:\Nant\bin, which is Nant.exe. This exe will be responsible for executing your scripts. So <o:p />

i. Either you have to dump your scripts to C:\Nant folder or<o:p />

ii. Add the path (C:\Nant\bin) to your PATH environment variable.<o:p />

<o:p />In this article I will follow the first approach.<o:p />

<o:p /> Using the code

A "Hello World" Example<o:p />

Consider the following very simple NAnt script:<o:p />

<?xml version="1.0" encoding="utf-8" ?><o:p />

<project name="HelloWorld" default="go"><o:p />

<property name="message" value="Hello World!"/><o:p />

<target name="go"><o:p />

<echo message="${message}"/><o:p />

</target><o:p />

</project><o:p />


Save this script as and then do one of two things. Either navigate to the<o:p />

directory in which the file is saved and type<o:p />

Nant<o:p />

or use an explicit path to the file at the command prompt such as<o:p />

// nant -f:D:\BookCode\Chapter2\


You will get the following output:<o:p />


---------- NAnt ----------<o:p />

NAnt 0.85<o:p />

Copyright (C) 2001-2003 Gerry Shaw<o:p /><o:p />

Buildfile: file:///<o:p />

Target(s) specified: go<o:p />

go:<o:p />

[echo] Hello World!<o:p />


Total time: 0 seconds.<o:p />

Output completed (0 sec consumed) - Normal Termination

The Project<o:p />

The project node is the root node of a build file. This node can contain any number of<o:p />

<property> nodes, <task> nodes, and <target> nodes. Generally, the rest of the build file is<o:p />

now defined either as a property, or as a target, or as a task within a target.<o:p />


Project Node attributes: <o:p />


name The name of the project.<o:p />

default The default target to use when no target is supplied.<o:p />

basedir The base directory from which all path calculations are done. No<o:p />

Current directory is the default.<o:p />



The Target<o:p />

Targets are used to "modularize" the build file. A target contains zero or more tasks to be completed<o:p />

in a sequence.<o:p />

Target Node attributes <o:p />


name<o:p />

The name attribute is crucial to the target, as the target is invoked by name. Simple as that.<o:p />

description<o:p />

The description is shown when the –projecthelp switch is used at the command line, and so<o:p />

can be of some assistance to a user of the build file.<o:p />

depends<o:p />

Targets can be made to depend on each other by use of the depends attribute. This attribute<o:p />

takes a comma-delimited list of targets that are to be executed prior to the execution of the<o:p />

target in question. For example:<o:p />

<target name="go" depends="foo, bar"/><o:p />

This means that target go will not execute until target foo and then target bar have been executed.<o:p />

Additionally, any targets that foo or bar depend on must be executed. You will be pleased<o:p />

to note that NAnt can figure out circular dependencies so that the following build file will not<o:p />

execute:<o:p />



Properties :<o:p />

These are similar to the variables used in a programming language. They are used as a key value pair.<o:p />



Important tasks with NAnt:<o:p />


NUnit: <o:p />

Nant can be used to run unit test suite written in NUnit framework. Nant provides the <Nunit2> element for this purpose. (Note that here 2 stands for version 2 of nunit framework) . The sample script is as follows:<o:p />


<?xml version="1.0"?><o:p />

<project name="testNUnit"><o:p />

<nunit2><o:p />

<formatter<o:p />

type="Xml"<o:p />

usefile="true"<o:p />

extension=".xml"<o:p />

outputdir="C:\NAnt\NUnitResults\" /><o:p />

<test assemblyname="E:\testing\TestClassLibrary1\\bin\Debug\TestClassLibrary1.dll" /><o:p />

</nunit2><o:p />

</project><o:p />



NDoc:<o:p />


NDoc is a tool that can be used to create extremely presentable<o:p />

MSDN-style documentation in web or compiled HTML (CHM) formats from the<o:p />

XML documentation capabilities of the C# language.<o:p />

NAnt comes with a version of the core<o:p />

NDoc assembly, and this task can be used to perform the same action:<o:p />


<?xml version="1.0"?><o:p />

<project name="TestNDoc"><o:p />

<ndoc><o:p />

<assemblies basedir="E:\testing\ClassLibrary1\ClassLibrary1\bin\Debug\"><o:p />

<include name="ClassLibrary1.dll" /><o:p />

</assemblies><o:p />

<summaries><o:p />

<include name="ClassLibrary1.xml" /><o:p />

</summaries><o:p />

<documenters><o:p />

<documenter name="MSDN"><o:p />

<property name="OutputDirectory" value="C:\MyDocs\NDOC" /><o:p />

<property name="HtmlHelpName" value="MyProject" /><o:p />

<property name="ShowMissingSummaries" value="True" /><o:p />

<property<o:p />

name="HtmlHelpCompilerFilename" value="hhc.exe" /><o:p />

<property name="IncludeFavorites" value="False" /><o:p />

<property name="Title" value="MySystem (NDoc)" /><o:p />

<property name="SplitTOCs" value="False" /><o:p />

<property name="DefaulTOC" value="" /><o:p />

<property name="ShowVisualBasic" value="False" /><o:p />

<property name="ShowMissingSummaries" value="True" /><o:p />

<property name="ShowMissingRemarks" value="False" /><o:p />

<property name="ShowMissingParams" value="True" /><o:p />

<property name="ShowMissingReturns" value="True" /><o:p />

<property name="ShowMissingValues" value="True" /><o:p />

<property name="DocumentInternals" value="True" /><o:p />

<property name="DocumentProtected" value="True" /><o:p />

<property name="DocumentPrivates" value="False" /><o:p />

<property name="DocumentEmptyNamespaces" value="False" /><o:p />

<property name="IncludeAssemblyVersion" value="True" /><o:p />

<property name="CopyrightText" value="Etomic 2005" /><o:p />

<property name="CopyrightHref" value="" /><o:p />

</documenter><o:p />

</documenters><o:p />

</ndoc><o:p />

</project><o:p />


The above script generates the ndoc for the assembly classlibrary1 which is present in the base directory mentioned.<o:p />



FxCop:<o:p />

Nant can be used to automate fxcop command. The following script explains how: <o:p />


<?xml version="1.0"?><o:p />

<project name="testFxCop"><o:p />

<exec<o:p />

program="C:\Program Files\Microsoft FxCop 1.35\FxCopCmd.exe"<o:p />

commandline="/f:E:\testing\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll /o:fxcop.xml"<o:p />

failonerror="false" /><o:p />

</project><o:p />

Replace value of program attribute to whatever is the version of fxcop executable in your machine. <o:p />

Mention the path for assembly in commandline, along with name of the output file desired.<o:p />

Faileonerror indicates whether to stop the process once an error is encountered. <o:p />



NCover: <o:p />

<project name="testNCover" default="coverage" failonerror="true"><o:p />

<loadtasks assembly="NCoverExplorer.NAntTasks.dll" /><o:p />

<target name="coverage" description="Code coverage test run."><o:p />

<ncover<o:p />

program="C:\Program Files\NCover\Ncover.console.exe" <o:p />

commandLineExe="C:\Program Files\NUnit-Net-2.0 2.2.8\bin\nunit-console.exe" <o:p />

commandLineArgs="E:\REL\Test\TestTest\bin\Debug\TestTest.dll"<o:p />

logLevel="Verbose"<o:p />

excludeAttributes="CoverageExcludeAttribute"><o:p />

<assemblies basedir="E:\REL\Test\Test\bin\Debug"><o:p />

<!-- include this to have only the test.dll reviewed and not the TestTest.dll --><o:p />

<include name="Test.dll"/> <o:p />

</assemblies><o:p />


<o:p />

To exclude a selected class or method from being reviewed by ncoverage, use the CoverageExclude attribute.<o:p />

public class CoverageExcludeAttribute : Attribute { }<o:p />

namespace MyApp<o:p />

{<o:p />

[CoverageExclude]<o:p />

Partial class MyClass<o:p />

{<o:p />


}<o:p />

}<o:p />

Add the CoverageExclude attribute to your file , either on top of class or method. <o:p />

This will exclude the designated method or class from being reviewed.<o:p />


I hope this article has given you a basic idea about writing Nant scripts for cruise control integration. You can get more documenation and online help at

Thill then happy scripting !!!.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThe best Pin
Frygreen12-Aug-08 8:22
memberFrygreen12-Aug-08 8:22 
GeneralRe: The best Pin
prst12313-Aug-08 20:13
memberprst12313-Aug-08 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 10 Aug 2007
Article Copyright 2007 by prst123
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid