Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
Properties
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.

Property

Description

nant.project.basedir

The absolute path of the project's basedir

nant.project.buildfile

The absolute path of the buildfile

nant.settings.currentframework

The current target framework

nant.settings.currentframework.sdkdirectory

The SDK directory of the current target framework


Tasks
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

Description

<copy>

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

<delete>

Deletes a file, a set of files or a directory

<mkdir>

Creates a directory

<attrib>

Modifies file attributes

 .NET Tasks

Description

<vbc>

Compiles Visual Basic .NET files

<csc>

Compiles C# files

<solution>

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

<tlbimp>

Imports a type-library to a .NET assembly

<regsvcs>

Installs or removes .NET Services

 Misc Tasks

Description

<mail>

Sends an STMP message

<zip>

Creates a zip file from the specified file list

<unzip>

Extracts a zip package

<ndoc>

Runs NDoc to create the documentation

<nunit2>

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.

Attributes

Description

failonerror

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

if

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

unless

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

verbose

Determines whether the task will report detailed log messages


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

Attributes

Description

name

The name of the target

depends

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

if

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

unless

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

description

A short description of the target's purpose


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

Attributes

Description

name

The name of the project

default

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

basedir

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.

 

 

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