NANT

Welcome to the world of DOT NET Build Scripting

By - scmGalaxy.com

About Me !

What is NANT?

Another neat Build tool for DOT NET

Build Tool

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

NANT

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.

Advantage

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

Requirement

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

Download

Latest Release - 0.92

http://sourceforge.net/projects/nant/files/nant/0.92/

Install

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:..\ProjectName.build
    Runs NAnt using the ProjectName.build 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:..\ProjectName.build 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

nant -help

Scripting in NANT

Terminology

  • 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!" />
</project>

Using target

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

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

</project>

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.

Projects

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

Description

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

Targets

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

Task

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 - http://nant.sourceforge.net/release/0.85/help/tasks/index.html

Lets Start real time project scripting

https://beta.etherpad.org/p/hcltraining
<project name="Hello World" default="build" basedir="." xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd">
        
        <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>
        
        <target name="build" description="compiles the source code">
            <csc target="exe" output="HelloWorld.exe" debug="${debug}">
                <sources>
                    <includes name="HelloCS.cs" />
                </sources>
            </csc>
        </target>
    </project>

Compiled Project

HelloCS.cs

// 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#!");
   }
}

build.bat

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 - http://nant.sourceforge.net/release/0.85/help/fundamentals/properties.html

Dependencies

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.

Tasks

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

Condition

<?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}"/>
</target>

</project>

exec

<exec program="ping"> 
	<arg value="google.com" /> 
</exec>


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

Task Reference

http://nant.sourceforge.net/release/0.85/help/tasks/index.html

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

Advance

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

<solution configuration="release"> 
<projects> 
	<includesfile name="projects.txt" /> 
</projects> 
</solution>
More - http://nant.sourceforge.net/release/0.85-rc1/help/tasks/solution.html

NANT monitored using...

  • Listeners
  • loggers

Listeners

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

Loggers

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="smtp.wherever.be" />
        <property name="MailLogger.from" value="me@telenet.be" />
        <property name="MailLogger.failure.notify" value="true" />
        <property name="MailLogger.success.notify" value="true" />
        <property name="MailLogger.failure.to" value="support@home.be" />
        <property name="MailLogger.success.to" value="support@home.be" />
        <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" />
        </fileset>

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

NUnit

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

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

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>

Reference Project

Reference

http://www.codeproject.com/Articles/38718/Building-and-Deploying-Using-NAnt

http://www.4guysfromrolla.com/articles/120104-1.aspx

http://www.codeproject.com/Articles/19964/Introduction-to-NAnt

http://nant.sourceforge.net/release/0.85/help/tasks/index.html

Questions ???

Thank You !!