NANT is a free .NET build tool (http://sourceforge.net/projects/nant/) that is like MAKE, only better. NANT's build files are written in XML and the tasks that make up the build are implemented using objects written in .NET. The combination of XML and objects make NANT an extremely flexible and effective utility for automating build processes. It is flexible enough to be used for more than just compiling and building products.
NANT is not difficult to use, but getting started can be challenging. My goal is to briefly describe how to use NANT. To build a C# project using NANT, I will use four NANT elements. Three of the elementsproject, property, and targetare NANT constructs; the fourth is the CSC task that compiles C# code.
The project element is required by NANT to contain everything else. The project can contain targets, properties, and tasks. Projects can have three attributesname, default, and basedir. Only the default attribute is required. The default attribute is the name of the first target that gets called. When NANT runs the build script, it executes all of the tasks and properties outside of targets before calling the default target.
The property element is how you declare variables in NANT. Properties can be used in attributes in a build script. This is done by placing the property name inside of "${" and "}". For instance, if you have a property called foo you use it like: ${foo}.
Targets are NANT's subroutines. You place most of your tasks within a target to organize your build script. Each target requires a name attribute. Targets can also have dependencies that cause the dependent target to be executed before the target executes.
The csc task compiles C# code using the csc compiler supplied with the .NET SDK. The csc task has several attributes, but the main ones to know are target, debug, and output. Targets determines whether the compiler produces an EXE or DLL. If the debug attribute is true, the compiler produces debug symbols. The output attribute supplies the name that the assembly produces. The csc task also needs references, sources, and optional resources. These are supplied by subelements that go inside the csc task.
To compile a C# program, the compiler must be told what other assemblies the program uses. This is done using the references tag. Each referenced DLL is added individually by putting its name inside an include element. For instance:
<references>
<includes name="mydll.dll" />
</references>
You must provide the complete path to the assembly being referenced or the compiler can't find it.
Source files and resources are handled using filesetsNANT objects that represent a set of files. The set of files can be made by either explicitly adding files to the set or using wildcards. The fileset has a basedir attribute that controls the base directory of all the files paths are taken from. To add all of the C# files in the "C:\MyProgram" directory to your sources element, you do the following:
<sources basedir="C:\MyProgram">
<includes name="*.cs" />
</sources>
If MyProgram has a directory structure underneath it that needs to be included, you can use the following:
<sources basedir="C:\MyProgram">
<includes name="**/*.cs" />
</sources>
Normally, wildcards do not match or chase down directories. NANT added the ** operator for that purpose. This makes it much easier to deal with complex directory structures.
Listing One contains the entire build script. The build script has a project whose default target is myprogram.build. The project has a single property debug. The value of debug is set to true. The myprogram.build target contains the csc task, which compiles the C# program and names the output "myprogram.exe."
To run this build, save the NANT script in a file called "myprogram.build." Then start Visual Studio's command-line shell. Add the path to NANT's bin directory to your environment. Change to the project directory and run NANT, which automatically searches a file that ends with ".build" and run its default target. You can also specify a build file by adding the /f parameter.
Currently, NANT provides 58 tasks in its main distribution and 42 tasks in the NANTContrib package. You can also write your own tasks for anything not already implemented.
J.W.
Back to Article