A target has these attributes:

Attribute Description Required
name The name of the target. 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

The optional description attribute can be used to provide a one-line description of this target, which is printed by the -projecthelp command-line option.


A target can depend on other targets.  You might have a target for compiling, for example, and a target for creating a distributable.  You can only build a distributable when you have compiled first, so the distribute target depends on the compile target.  NAnt resolves these 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.

A target gets executed only once, even when more than one target depends on it (see the previous example). However, when the <call> task is used to execute a target, both the target and all its dependent targets will be re-executed.

Wild Targets

A target can be marked as wild by setting the name attribute to "*". A build file can contain up to one wild target, and it is executed if and only if the invoked target does not exist in the current build file.  Wild targets let you define how to handle invalid requests, or provide generic behavior for unknown targets. For example:

     <target name="A" />
     <target name="B" />
     <target name="*" />

The last target is executed if the invoked target is neither A nor B.

Conditional Execution

A target also has the ability to perform its execution if or unless a property has been set.  This allows, for example, better control on the building process depending on the state of the system (OS, command-line property defines, etc.).  To make a target sense this property, you should add the if or unless attribute with an expression that the target should react to. For example:

    <target name="build-module-A" if="${module-A-present}" />
    <target name="build-own-fake-module-A" unless="${file::exists('fake-module-a.dll')}" />

If no if and no unless attribute is present, the target will always be executed.

Note: the dependencies of a target are always executed before testing the target's condition.