Welcome to the world of DOT NET Build Scripting

By -

About Me !

What is NANT?

Another neat Build tool for DOT NET

Build Tool

  • To Setup env
  • To compile
  • To Package
  • To deploy


The configuration files are XML-based, calling out a target tree where various tasks get executed. Each task is run by an object that implements a particular Task interface.


  • cross-platform
  • Wide collection of Tasks
  • Free
  • Open source


To use NAnt you need one of the following CLR's:
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 2.0
  • Mono 1.x

Library Dependencies

  • NUnit - Required for unit testing
  • NDoc - Required for documentation generation
  • SharpZipLib - Required for the zip and unzip tasks


Latest Release - 0.92


Run NAnt using Microsoft.NET
  • Create a file called nant.bat in a directory that is included in the PATH system environment variable. (eg. C:\WINDOWS).
  • Add the following to nant.bat:

@echo off
"C:\Program Files\NAnt\bin\NAnt.exe" %*

How to Compile it?

  • NAnt
    Runs NAnt using the file ending in *.build file in the current directory, on the default target.
  • NAnt -buildfile:..\
    Runs NAnt using the file in the parent directory, on the default target.
  • NAnt clean
    Runs NAnt using the default build file in the current directory, on a target called clean.
  • NAnt -buildfile:..\ clean
  • NAnt -D:debug=false clean dist Runs NAnt using the default build file in the current directory, first on the clean target and then on the dist target while setting the debug property to false. This could, for example, make a release distribution from scratch.


nant -help

Scripting in 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.

Basic Program

<?xml version="1.0" encoding="utf-8" ?>
<project name="HelloWorld">
	<echo message="Hello, World!" />

Using target

 <?xml version="1.0" encoding="utf-8" ?>
<project name="HelloWorld" default="message" basedir="." >
<target name="message">
	<echo message="Hello, World!" />

Using Property

<?xml version="1.0" encoding="utf-8" ?>
<project name="HelloWorld" default="go">
<property name="message" value="Hello World!"/>

<target name="go">
	<echo message="${message}"/>


4. Ant Users

  • Tasks do not have to be in a target. Tasks that appear directly in the project are executed inorder before any tasks in targets are executed.
  • nant.onsuccess and nant.onfailure properties can be defined target names that will be executed at the end of the build.
  • NAnt looks for the first file ending in .build instead of build.xml.


A project has these attributes:
Attribute Description Required
name The name of the project. No
default The default target to execute when no target is supplied on the command-line. No
basedir The base directory from which all path calculations are done. If not set, the parent directory of the buildfile will be used. No


a description for the project can be provided as a top-level <description> element

<?xml version="1.0" encoding="utf-8" ?>
<project name="HelloWorld">
	<description>This is a description.</description>
	<echo message="Hello, World!" />


A target has these attributes:
Attribute Description Required
name The name of the project. Yes
depends A comma-separated list of names of targets on which this target depends. No
if An expression that should evaluate to true in order for this target to execute. No
unless An expression that, when evaluated to true, will cause the target to be skipped. No
description A short description of this target's function. No


A task is a piece of code that can be executed.

Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />

List of all Tasks -

Lets Start real time project scripting
<project name="Hello World" default="build" basedir="." xmlns="">
        <description>The Hello World of build files.</description>
        <property name="debug" value="true" overwrite="false" />
        <target name="clean" description="remove all generated files">
            <delete file="HelloWorld.exe" failonerror="false" />
            <delete file="HelloWorld.pdb" failonerror="false" />
        <target name="build" description="compiles the source code">
            <csc target="exe" output="HelloWorld.exe" debug="${debug}">
                    <includes name="HelloCS.cs" />

Compiled Project


// Allow easy reference to the System namespace classes.
using System;

// This class exists only to house the entry point.
class MainApp {
   // The static method, Main, is the application's entry point.
   public static void Main() {
      // Write text to the console.
      Console.WriteLine("Hello World using C#!");


csc.exe /debug+ /out:.\HelloCS.exe helloCS.cs

Global Property

Properties that should be accessible to all build files on a system can be defined in the <properties> node of the NAnt configuration file (NAnt.exe.config).

More -


NAnt tries to execute the targets in the depends attribute in the order they appear from left to right.

It is possible that a target can get executed earlier when an earlier target depends on it:

<target name="A"/> 
<target name="B" depends="A" /> 
<target name="C" depends="B" /> 
<target name="D" depends="C,B,A" />
Suppose we want to execute target D. From its depends attribute, you might think that first target C, then B and then A is executed. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.


  • <csc/> for calling the C# compiler, <exec/>
  • <exec/> for executing just about any executable you can call from the command-line
  • <if/> for nesting conditional tasks
  • <call/> for calling other targets
  • <mkdir/> for making directories
  • <echo/> for outputting messages during the execution of the build file.


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

<project name="HelloWorld" default="go">
<property name="message" value="Hello World!"/>
<property name="module-A-present" value="True"/>

<target name="go" if="${module-A-present}">
	<echo message="${message}"/>



<exec program="ping"> 
	<arg value="" /> 

<exec program="CMD.EXE" commandline="/C DatabaseInstallation.cmd" workingdir="${installPath}"> 
<arg value="${Database_ServerInstanceName}" /> 
<arg value="${Database_Name}"/> 
<arg value="${Database_Domain}"/> 
<arg value="${Database_DtsPackagesFolder}"/> 

Task Reference

Set Run time property

To override properties specified in the build file use the -D:property=value option, where property is the name of the property, and value is the value for that property.

LAB & Exercise


<solution configuration="release" solutionfile="test.sln" />

<solution configuration="release"> 
	<includesfile name="projects.txt" /> 
More -

NANT monitored using...

  • Listeners
  • loggers


  • A listener is alerted of the following events:
  • build started
  • build finished
  • target started
  • target finished
  • task started
  • task finished
  • message logged


Loggers extend the capabilities of listeners and add the following features:

- Receives a handle to the standard output and error print streams and therefore can log information to the console or the -logfile specified file.

- Logging level (-quiet, -verbose, -debug) aware.

NAnt -logger:NAnt.Core.DefaultLogger
<project name="MailLogger Test" default="build">
        <property name="MailLogger.mailhost" value="" />
        <property name="MailLogger.from" value="" />
        <property name="MailLogger.failure.notify" value="true" />
        <property name="MailLogger.success.notify" value="true" />
        <property name="" value="" />
        <property name="" value="" />
        <property name="MailLogger.failure.subject" value="Nightly build failure !" />
        <property name="MailLogger.success.subject" value="Nightly build successful" />
        <property name="MailLogger.failure.attachments" value="MailLogger.failure.files" />
        <property name="MailLogger.success.attachments" value="MailLogger.success.files" />

        <!-- set of files to attach when build fails -->
        <fileset id="MailLogger.failure.files">
            <include name="dump.log" />
            <include name="trace.txt" />

        <!-- set of files to attach when build is successful -->
        <fileset id="MailLogger.success.files">
            <include name="trace.txt" />
        <target name="build">
            <echo message="Starting build" />
            <echo message="Finished build" />


Nant can be used to run unit test suite written in NUnit framework. Nant provides the element for this purpose.

Sample Program

<?xml version="1.0"?>
<project name="testNUnit">

	<formatter type="Xml" usefile="true" extension=".xml" outputdir="C:\NAnt\NUnitResults\" />
	<test assemblyname="E:\testing\TestClassLibrary1\\bin\Debug\TestClassLibrary1.dll" />



  • 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">
<assemblies basedir="E:\testing\ClassLibrary1\ClassLibrary1\bin\Debug\">
	<include name="ClassLibrary1.dll" />
	<include name="ClassLibrary1.xml" />
<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="" />



<project name="testNCover" default="coverage" failonerror="true">
<loadtasks assembly="NCoverExplorer.NAntTasks.dll" />

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

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

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

Reference Project


Questions ???

Thank You !!