Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Building .NET Applications with NAnt : Page 2

Although Visual Studio .NET does a number of things very well to assist in the development process, there are certainly areas that need improvement. One of those areas is dealing with a build process that requires something a little more than just compiling source code.




Application Security Testing: An Integral Part of DevOps

A build script can contain zero to many property definitions. A property in NAnt is analogous to a variable. You define a property by the use of a <property> tag with a name and value attribute.

<property name="src.dir" value="source" />

To access a property value, you place the name of the property between the identifiers ${" and "}. For example, to access the src.dir property declared earlier, specify the following:

<echo message="Source directory: ${src.dir}" />

Default property values are defined within the build script. If you need to override any property value, you can specify the -D command-line flag and supply the new property value.

C:> Nant ?D:src.dir=NotTheDefaultSourceDirectory

Properties can be set as read only by setting the Readonly attribute on the <property> tag to True.

<property name="company.name" readonly="true" value = "Solution Partners, Inc." />

You can create property values that are global to all build scripts on your computer by defining the property definitions in NAnt's configuration file (nant.exe.config, found in the NAnt \bin directory) under the <properties> configuration section as shown below.

<?xml version="1.0"?> <configuration> ... <nant> ... <properties> <!-- properties defined here are accessible to all build files --> <property name="company.name" value = "Solution Partners, Inc." readonly="true" /> </properties> </nant> </configuration>

NAnt has a few built-in properties that can be used within your build script; a sampling of these properties can be found in Table 1.

Table 1: These are a sampling of built-in NAnt properties.




The absolute path of the project's basedir


The absolute path of the buildfile


The current target framework


The SDK directory of the current target framework

Tasks define a command to be executed. Tasks are XML elements with a set of defined attributes and child elements that prescribe the options for the task. For example, if you want to execute a command to delete a directory, use the <delete> task.

<delete dir="bin" failonerror="false" />

The dir attribute is specific to the <delete> task and sets the option for the <delete> task to delete the directory named bin. The Failonerror attribute is common to all tasks. If the task fails, the build also fails, although there are some cases where a failed task doesn't necessarily mean the build fails. The default value is True.

If you want your build script to send an e-mail to the development team when the build completes, you could use the <mail> task.

<mail from="buildmaster@foo.com " tolist="devteam@foo.com" subject="Build #${build.version} complete" mailhost="smtp.foo.com"> <files> <include name="*.txt" /> </files> <attachments> <include name="*.zip" /> </attachments> </mail>

Tasks define the power of NAnt. There are over 70 tasks included in the NAnt core tool. A small sampling of some the available tasks is listed in Table 2. Additional tasks are available through external libraries, such as NAntContrib. If the available tasks don't quite get the job done, you can create custom tasks through the extensible architecture.

The NantContrib project provides an assortment of additional tasks for NAnt build scripts.
Table 2: These are just some of the tasks available in the NAnt core tool.

File System Tasks



Copies a file or set of files to a new file or directory


Deletes a file, a set of files or a directory


Creates a directory


Modifies file attributes

 .NET Tasks



Compiles Visual Basic .NET files


Compiles C# files


Compiles Visual Studio .NET solutions (or sets of projects). Determines project dependencies from inter-project references


Imports a type-library to a .NET assembly


Installs or removes .NET Services

 Misc Tasks



Sends an STMP message


Creates a zip file from the specified file list


Extracts a zip package


Runs NDoc to create the documentation


Runs tests using the NUnit v2.2 framework

Although each task has its own custom attributes defined to support its execution, every task has a common set of attributes. These are listed in Table 3.

Table 3: These are task common attributes.




If set to True, stops the build if this task fails. Otherwise, it just reports.


An expression that should evaluate to True for this target to execute


An expression that causes the target to be skipped when it evaluates to True


Determines whether the task will report detailed log messages

The <target> tag defines a collection of tasks to execute. They usually represent a particular step in the build process and can have dependencies on other targets. The If and Unless attributes allow for conditional target execution. When the If attribute is defined, the target only executes if the expression evaluates to True. If the Unless attribute is defined, the target is skipped when the Unless expressions evaluates to True.

Targets can be dependent on other targets. For example, if within the build script you want to clean out the output directory before the build, you might define something like this:

<target name="clean"> <delete dir="bin" failonerror="false" /> <mkdir dir="bin" /> </target>

By specifying a dependency with the Depends attribute, the target will not execute until all of its dependency targets have executed first. One of the nice features of NAnt is that it executes a target only once. So if multiple targets have a dependency on the same target, that dependent target executes only once.

<target name="clean"> <delete dir="bin" failonerror="false" /> <mkdir dir="bin" /> </target> <target name="build" depends="clean"> ... </target>

Table 4 lists the attributes and the descriptions for the <Target> tag. Only the Name attribute is required.

Table 4: Here's a list of <Target> attributes.




The name of the target


A comma-separated list of names and targets on which the target depends


An expression that should evaluate to True for this target to execute


An expression that causes the target to be skipped when it evaluates to True


A short description of the target's purpose

The <project> tag is the main container for the build script. The important element of the <project> tag (other than allowing you to define a well-formed XML document) is the use of the Default attribute. When no target is specified on the command line execution of the build script, NAnt executes the target specified in the Default attribute. Table 5 lists the attributes and their descriptions for the <project> tag. None of the attributes are required.

<project name="CalcApp" default="build"> <property name="src.dir" value="source" /> <property name="output.dir" value="bin" /> <target name="clean"> <delete dir="${output.dir}" failonerror="false" /> <mkdir dir="${output.dir}" /> </target> <target name="build" depends="clean"> ... </target> ... </project>

Table 5: Here's a list of <Project> attributes.




The name of the project


An expression that should evaluate to True for this target to execute


The base directory from which all paths are determined. If it isn't specified, the build script's parent directory is used.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date