Using NAntNow that you are convinced that you need to use NAnt, how do you use the thing, anyway? Well, it is easy to get started with a few projects, but as your project gets larger, the build will get more complicated. A little design time up front will save you a lot of time in the end. There are basically only three important concepts to understanding NAnt build projects:
Figure 4-1 illustrates how these concepts interact with one another.NOTEAppendixes A and B list the built-in tasks available in NAnt and NAntContrib.The following snippet is a simple Hello World program in C# that we will build using NAnt:using System; namespace Simple { class HelloWorld { [STAThread] static void Main(string[] args) { Console.WriteLine("Hello World."); } } } Here is the NAnt build file for compiling the obligatory Hello World application:<project name="Simple" default="build"> <target name="build" description="compiles the source code"> <csc target="exe" output="helloworld.exe"> <sources> <includes name="HelloWorld.cs"/> </sources> </csc> </target> </project> While this may seem like overkill compared to a simple compiler command line (below), as the builds get more complex and add multiple references to dependent assemblies, you will quickly see the usefulness of a NAnt build over a command-line compile.csc /target:exe helloworld.cs In order to build the Hello World C# file using the NAnt build here, you must know how to execute NAnt. Executing NAntListing 4.1 shows the output if you were to run nAnt.exe help. Listing 4.1. NAnt Command-Line Parametersusage: nant [options] [target [target2 [target3] ... ]] options: -help print this message -projecthelp print project help information -buildfile:<file> use given buildfile -find search parent directories for buildfile -D:<property>=<value> use value for given property -k defaultframework. -verbose displays more information during build process -logger: use given class name as logger -logfile:, -l: use value as name of log output file A file ending in .build will be used if no buildfile is specified. By default, NAnt looks for a .build file in the current directory to build. If one does not exist or if more than one .build file exists, then the buildfile option must be used to specify the exact location of the intended build file.The D options allow users to set properties that are used in the build. This allows the user to override the default values of properties in the build.A NAnt project allows for setting a default target to be run if no target is specified. By using inter-target dependencies, a number of targets could automatically be executed. Alternatively, the NAnt tool allows a sequential list of targets to be specified at execution. |
