Nant is A. Net-based generation tool. Unlike the current version of Visual Studio. NET, it makes the creation and generation process of your project very easy. When you have a large number of developers engaged in a single project, you cannot rely on generating from the seat of a single user. You do not want to manually generate the project on a regular basis. You are more willing to create an automatic generation process that runs every night. Nant allows you to generate solutions, copy files, run nunit tests, send emails, and so on. Unfortunately, Nant lacks a beautiful graphical interface, but it does have console applications and XML files that can specify which tasks should be completed during the generation process. Note that msbuild (a new generation platform of Visual Studio 2005) Prepares for each robust generation scheme and is driven in a similar way by XML-based project files.
Actually running Nant
In this example, I will create an Nant version file for the previously created nunitexample solution. First, I need to create an XML file with the. Build extension, put it in the root directory of my project, and then add an XML declaration to the top of the file. The first tag I need to add to this file is the project Tag:
xml version="1.0"?>
<project name="NUnit Example" default="build" basedir=".">
<description>The NUnit Example Projectdescription>
project>
The project tag is also used to set the project name, default target, and base directory. The description mark is a brief description of the project.
Next, I will add the property tag, which can be used to store the settings to a single location (which can then be accessed from any location in the file ). In this example, I will create an attribute named Debug. I can then set it to true or false to reflect whether I want to compile the project under the debug configuration. (Finally, this specific property does not really affect how to generate the project; it is just a variable you set and will be read when you are actually sure how to generate the project .)
Next, I need to create a target tag. A project can contain multiple targets that can be specified during Nant running. If no target is specified, the default target is used (the target set in the project element ). In this example, the default target is build. Let's take a look at the target element, which will contain most of the generated information:
<Target name = "build" Description = "compiles the source code">
Target>
In the target element, I will set the target name to build and create instructions on what the target will do. I will also create a CSC element to specify the data that should be passed to the CSC C # compiler. Let's take a look at the CSC element:
<csc target="library" output=". indebugNUnitExample.dll"
debug="${debug}">
<references>
<includes name="C:program filesNUnit V2.1 inNUnit.Framework.dll"/>
references>
<sources>
<includes name="HashtableTest.cs"/>
sources>
csc>
First, I must set the target of the CSC element. In this example, I will create a. dll file, so I set target to library. Next, I must set the output of the CSC element, which is the location of the. dll file to be created. Finally, I need to set the debug attribute to determine whether to compile the project in debugging. Because I created an attribute to store this value, I can use the following string to access the value of this attribute: $ {debug }. The CSC element also contains child elements. I need to create two elements: the references element will tell Nant which assembly should be referenced for this project, and the sources element will tell Nant what files should be included during the generation process. In this example, I reference the nunit. Framework. dll assembly and include the hashtabletest. CS file. The following code block shows the complete file generation. (You usually need to create a clean target to delete the generated file, but I have omitted it for the sake of simplicity .)
XML version = "1.0"?>
<Project name = "nunit example" default = "build" basedir = ".">
<Description> the nunit example projectdescription>
<Property name = "debug" value = "true"/>
<Target name = "build" Description = "compiles the source code">
<CSC target = "library" output = ". indebugnunitexample. dll"
DEBUG = "$ {debug}">
<References>
<Shortdes name = "C: Program filesnunit
V2.1 innunit. Framework. dll "/>
References>
<Sources>
<Shortdes name = "hashtabletest. cs"/>
Sources>
CSC>
Target>
Project>
To generate this file, I need to go to the root directory of my project (where the generated file is located), and then execute nant.exe from this location. If the file is successfully generated, you can find the. dll and. PDB files in the bin directory of the application. Although Nant is certainly not as simple as clicking build in Visual Studio, it is still a very powerful tool that can be used to develop a build process that runs on an automatic schedule. Nant also includes some useful functions, such as the ability to run unit tests or copy additional files (these functions are not supported by the current Visual Studio generation process ).
Nant is an open source project and can be downloaded from a http://nant.sourceforge.net.