Introduction to NAnt
The following exercise introduces NAnt, a build tool based on ANT, which is an XML-based Java build tool. NAnt enables us to define how we are going to build and test our .NET projects by using an XML file. The advantage this has over the batch file we just created is that it can be more configurable. We can define different build targets within the NAnt build file.At the time of this writing, NAnt is still in early stages of development, with version 0.84 being the latest release. So we are dealing with a piece of software that is not yet at version 1.0. Saying that, NAnt does have much of what is needed to build and test your project. You can download the latest version from http://nant.sourceforge.net/, and then we can get started and create a NAnt build file.To use NAnt, you need to have configured the path environment variable to include the Nant\bin directory. NAnt is a command-line tool; therefore, you also need to have the Visual Studio .NET (or .NET Framework if you are working without Visual Studio) environment variables registered in your command-line environment. There is a batch file called vsvars32.bat in the Microsoft Visual Studio .NET\Common7\Tools directory, which will most likely be in your Program Files directory. This batch file will set up your environment variables for you.
Exercise 7-2: Using NAnt to Automate the Build Process
We will start by building a simple C# Windows application project and setting up the NAnt build file for it.
1. Create a new C# Windows application project called NAntTest.2. In Solution Explorer, right-click the NAntTest project and select Add/Add New Item from the pop-up menu. Then select XML file and call it NAntTest.build (see Figure 7-2). The standard practice for NAnt build files is to call them the project name and use the extension .build. NAnt build files are XML files.
Figure 7-2. Add a new NAntTest.build file.
[View full size image]

Note
You can get the Visual Studio.NET IDE to display your build file as syntax-highlighted XML by right-clicking the file, selecting Open With, and then selecting th97/XML Editor.
4. Next we will add some properties to the project; these will be used by the other sections in the build file.
<?xml version="1.0" encoding="utf-8" ?>
<project name="NAntTest" basedir=".">
</project>
5. We can now define a target, nested within the project, for the build, as shown here.
<project name="NAntTest" basedir=".">
<property name="basename" value="NAntTest"/>
<property name="debug" value="true"/>
<property name="build.dir" value="build"/>
</project>
<?xml version="1.0" encoding="utf-8" ?>
<project name="NAntTest" basedir=".">
<property name="basename" value="NAntTest"/>
<property name="debug" value="true"/>
<property name="build.dir" value="build"/>
<target name="build">
<mkdir dir="${build.dir}"/>
<csc target="exe" output="${build.dir}\${basename}.exe"
debug="${debug}"
imports="System,System.Collections,
System.Data,System.Diagnostics,
System.Drawing,System.Windows.Forms"
rootnamespace="NAntTest" >
<sources>
<includes name="*.cs"/>
</sources>
<references>
<absolute file="System.dll"/>
<absolute file="System.Data.dll"/>
<absolute file="System.Drawing.dll"/>
<absolute file="System.Windows.Forms.dll"/>
<absolute file="System.XML.dll"/>
</references>
<arg value="/main:NAntTest.Form1"/>
</csc>
</target>
</project>
Build File Components
Let's examine what this file consists of:
- The target element has a name; we will use this later when we define more than one target. Then we use the mkdir element to make the build directory for the build output to go in. Notice we use the build.dir property we just defined in the previous step. The next element is csc for C# compiler options. We use the target attribute to state we are building an EXE rather than a DLL assembly. The output attribute defines the location and name of the output from the compilation. Again, notice we are using properties we defined earlier.
- The debug attribute uses the debug property we defined to indicate whether we are building the project for debug. Then the imports attribute defines all the assemblies to import into the project. The final attribute we use here is the rootnamespace attribute to define the root namespace of the code being compiled. For us that's the NAntTest project namespace.
6. After saving the build file, go to the command line and call NAnt to build the project (see Figure 7-3).
Figure 7-3. Our first NAnt build.
[View full size image]

Notice that we have added a description attribute to the target element. We then add the child delete element to the target. The dir attribute defines the directory to delete. The verbose attribute indicates whether the output should be explicit. The failonerror attribute indicates whether NAnt should consider it has failed if there is an error. Here we set this to false; we do so because if NAnt fails to delete the directory, we don't want to consider this an error because it might happen often when the directory is not there.9. Save the file and run NAnt from the command line, passing clean as a parameter (see Figure 7-4).
<target name="clean"
description="deletes the build directory">
<delete dir="${build.dir}" verbose="true"
failonerror="false"/>
</target>
Figure 7-4. Run NAnt using the clean target.
[View full size image]

11. Now we can define how to make release and debug builds by changing the debug property. Notice how these targets have a depends attribute; this defines the other targets on which they depend. When NAnt encounters this, it will build the other target first. So before building either for release or debug, NAnt will now build the clean target, which deletes any files previously built.
<project name="NAntTest" default="build" basedir=".">
12. We can get a debug and release build from this file by calling NAnt with the correct parameters, as shown in Figure 7-5.
<target name="debug" depends="clean">
<property name="debug" value="true"/>
</target>
<target name="release" depends="clean">
<property name="debug" value="false"/>
</target>
Figure 7-5. NAnt builds the release and build targets.
[View full size image]

14. We can now build and run the program using NAnt (see Figure 7-6).
<target name="run" depends="build">
<exec program="${build.dir}/${basename}.exe" />
</target>
Figure 7-6. Build and run the project from NAnt.
[View full size image]

Figure 7-7. Use NAnt to do a release build and run the application.
[View full size image]

