65.9K
CodeProject is changing. Read more.
Home

Introduction to NAnt

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.17/5 (8 votes)

Aug 10, 2007

6 min read

viewsIcon

57173

downloadIcon

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, Ant.

NAnt is an open source project, with a homepage at http://nant.sourceforge.net. There

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

not make it into the core NAnt release. Occasionally, tasks in NAntContrib will make the

switch to NAnt.

NAnt Nomenclature

Just before we dive in, let us clarify some of the terminology that we will use when discussing

NAnt:

NAnt: The physical executable (and the associated tasks) that form the application.

NAnt script/build script/build file: An XML file describing a project comprised of one or

more targets and zero or more properties.

Project: The root node of a build script. Each script has one project.

Target: A subdivision of a project. A target is a discrete piece of work that can consist of

zero or more tasks. Targets can be dependent on each other.

Task: A task is a defined activity that NAnt will execute; for example, creating a directory

or zipping a file. There are many built-in tasks to NAnt, and we will look at the most valuable

of these in Chapter 3.

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

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

Installation Instructions:

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

i. Either you have to dump your scripts to C:\Nant folder or

ii. Add the path (C:\Nant\bin) to your PATH environment variable.

In this article I will follow the first approach.

Using the code

A "Hello World" Example

Consider the following very simple NAnt script:

<?xml version="1.0" encoding="utf-8" ?>

<project name="HelloWorld" default="go">

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

<target name="go">

<echo message="${message}"/>

</target>

</project>


Save this script as HelloWorld.build and then do one of two things. Either navigate to the

directory in which the file is saved and type

Nant

or use an explicit path to the file at the command prompt such as



// nant -f:D:\BookCode\Chapter2\HelloWorld.build



// 

You will get the following output:

---------- NAnt ----------

NAnt 0.85

Copyright (C) 2001-2003 Gerry Shaw

http://nant.sourceforge.net

Buildfile: file:///HelloWorld.build

Target(s) specified: go

go:

[echo] Hello World!

BUILD SUCCEEDED

Total time: 0 seconds.

Output completed (0 sec consumed) - Normal Termination

The Project

The project node is the root node of a build file. This node can contain any number of

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

now defined either as a property, or as a target, or as a task within a target.

Project Node attributes:

name The name of the project.

default The default target to use when no target is supplied.

basedir The base directory from which all path calculations are done. No

Current directory is the default.

The Target

Targets are used to "modularize" the build file. A target contains zero or more tasks to be completed

in a sequence.

Target Node attributes

name

The name attribute is crucial to the target, as the target is invoked by name. Simple as that.

description

The description is shown when the –projecthelp switch is used at the command line, and so

can be of some assistance to a user of the build file.

depends

Targets can be made to depend on each other by use of the depends attribute. This attribute

takes a comma-delimited list of targets that are to be executed prior to the execution of the

target in question. For example:

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

This means that target go will not execute until target foo and then target bar have been executed.

Additionally, any targets that foo or bar depend on must be executed. You will be pleased

to note that NAnt can figure out circular dependencies so that the following build file will not

execute:

Properties :

These are similar to the variables used in a programming language. They are used as a key value pair.

Important tasks with NAnt:

NUnit:

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:

<?xml version="1.0"?>

<project name="testNUnit">

<nunit2>

<formatter

type="Xml"

usefile="true"

extension=".xml"

outputdir="C:\NAnt\NUnitResults\" />

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

</nunit2>

</project>

NDoc:

NDoc is a tool that can be used to create extremely presentable

MSDN-style documentation in web or compiled HTML (CHM) formats from the

XML documentation capabilities of the C# language.

NAnt comes with a version of the core

NDoc assembly, and this task can be used to perform the same action:

<?xml version="1.0"?>

<project name="TestNDoc">

<ndoc>

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

<include name="ClassLibrary1.dll" />

</assemblies>

<summaries>

<include name="ClassLibrary1.xml" />

</summaries>

<documenters>

<documenter name="MSDN">

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

<property name="HtmlHelpName" value="MyProject" />

<property name="ShowMissingSummaries" value="True" />

<property

name="HtmlHelpCompilerFilename" value="hhc.exe" />

<property name="IncludeFavorites" value="False" />

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

<property name="SplitTOCs" value="False" />

<property name="DefaulTOC" value="" />

<property name="ShowVisualBasic" value="False" />

<property name="ShowMissingSummaries" value="True" />

<property name="ShowMissingRemarks" value="False" />

<property name="ShowMissingParams" value="True" />

<property name="ShowMissingReturns" value="True" />

<property name="ShowMissingValues" value="True" />

<property name="DocumentInternals" value="True" />

<property name="DocumentProtected" value="True" />

<property name="DocumentPrivates" value="False" />

<property name="DocumentEmptyNamespaces" value="False" />

<property name="IncludeAssemblyVersion" value="True" />

<property name="CopyrightText" value="Etomic 2005" />

<property name="CopyrightHref" value="" />

</documenter>

</documenters>

</ndoc>

</project>

The above script generates the ndoc for the assembly classlibrary1 which is present in the base directory mentioned.

FxCop:

Nant can be used to automate fxcop command. The following script explains how:

<?xml version="1.0"?>

<project name="testFxCop">

<exec

program="C:\Program Files\Microsoft FxCop 1.35\FxCopCmd.exe"

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

failonerror="false" />

</project>

Replace value of program attribute to whatever is the version of fxcop executable in your machine.

Mention the path for assembly in commandline, along with name of the output file desired.

Faileonerror indicates whether to stop the process once an error is encountered.

NCover:


<project name="testNCover" default="coverage" failonerror="true">

<loadtasks assembly="NCoverExplorer.NAntTasks.dll" />

<target name="coverage" description="Code coverage test run.">

<ncover

program="C:\Program Files\NCover\Ncover.console.exe"

commandLineExe="C:\Program Files\NUnit-Net-2.0 2.2.8\bin\nunit-console.exe"

commandLineArgs="E:\REL\Test\TestTest\bin\Debug\TestTest.dll"

logLevel="Verbose"

excludeAttributes="CoverageExcludeAttribute">

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

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

<include name="Test.dll"/>

</assemblies>

</ncover>


To exclude a selected class or method from being reviewed by ncoverage, use the CoverageExclude attribute.

public class CoverageExcludeAttribute : Attribute { }

namespace MyApp

{

[CoverageExclude]

Partial class MyClass

{

}

}

Add the CoverageExclude attribute to your file , either on top of class or method.

This will exclude the designated method or class from being reviewed.

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