Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Introduction to NAnt

1.77/5 (11 votes)
10 Aug 20076 min read 1   408  
Writing NAnt scripts for NDoc, FxCop,NUnit. and NCoverage.

Introduction

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 http://nant.sourceforge.net. 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 http://nantcontrib.sourceforge.net. 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, http://nant.sourceforge.net. 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>

<o:p>
<o:p>

Save this script as HelloWorld.build 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\HelloWorld.build



// 

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

<o:p>

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

NAnt 0.85<o:p>

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

http://nant.sourceforge.net<o:p>

Buildfile: file:///HelloWorld.build<o:p>

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

go:<o:p>

[echo] Hello World!<o:p>

BUILD SUCCEEDED<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>

<o:p>

Project Node attributes: <o:p>

<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>

<o:p>

<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>

<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>

<o:p>

<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>

<o:p>

<o:p>

Important tasks with NAnt:<o:p>

<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>

<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>

<o:p>

<o:p>

NDoc:<o:p>

<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>

<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>

<o:p>

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

<o:p>

<o:p>

FxCop:<o:p>

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

<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>

<o:p>

<o:p>

NCover: <o:p>

<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>

</ncover>


<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>

}<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>

Summary:

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 http://nant.sourceforge.net..

Thill then happy scripting !!!.



License

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