NAnt is an XML-based free build tool for .NET applications. If you are a Java developer, you are probably familiar with Ant (http://ant.apache.org/). Ant has become an essential tool for almost any Java-based development. Developers have come to like Ant in favor of the build capabilities available in Java development tools. Based on the success of Ant, NAnt is an open source project that provides the capabilities of Ant to the world of .NET. Similar to Ant, to use NAnt, developers are expected to write a build file for their applications that contains a set of tasks (which can have dependencies on each other). When the NAnt build system is invoked, very similar to the "make," the tool compiles and/or executes the application. A key benefit of using a tool like NAnt is to establish automated build processes and times. For developers who prefer using the .NET Framework command-line tools, NAnt is an indispensable tool. To understand NAnt, take a simple scenario of how NAnt can be used to build and execute an application. As mentioned earlier, a NAnt build file is made up of a series of related tasks (called targets) identified by their names . Listing 6.1, for instance, has two targets: build and run. The run target is dependent on the build target. The build target, in turn , uses the command-line C# compiler to compile the source files into the executable using the <csc> task. The run target uses the < exec > task to run the executable. Listing 6.1 Simple NAnt Build File (default.build)<?xml version="1.0"?> <project name="Hello World" default="run"> <property name="debug" value="true"/> <target name="clean"> <delete> <fileset> <includes name="HelloWorld.exe "/> </fileset> </delete> </target> <target name="build"> <csc target="exe " output="HelloWorld.exe " debug="${debug}"> <sources> <includes name="HelloWorld.cs"/> </sources> </csc> </target> <target name="run" depends="build"> <exec program="HelloWorld.exe "/> </target> </project> After you have developed the build file (and the associated source code for your application), run NAnt. Make sure that you have NAnt's bin directory in your PATH variable. E:\hks\DotNet\Chapter 6>nant NAnt version 0.8.3 Copyright (C) 2001-2003 Gerry Shaw http://nant.sourceforge.net Buildfile: file:///E:/hks/DotNet/Chapter 6/default.build build: [csc] Compiling 1 files to E:\hks\DotNet\Chapter 6\HelloWorld.exe. run: [exec] HelloWorld.exe Hello Again Total time: 3 seconds. BUILD SUCCEEDED
The heart of the NAnt build engine is the series of tasks. Currently, a set of predefined tasks is available to automate most of the build process. Frequently used tasks and their brief functions are shown in Table 6.1. In addition, you can also develop your own tasks. Table 6.1. NAnt Predefined Tasks
For more information and/or to download NAnt, visit http://nant.sourceforge.net. |