eXtreme .NET: Introducing eXtreme Programming Techniques to .NET Developers [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

eXtreme .NET: Introducing eXtreme Programming Techniques to .NET Developers [Electronic resources] - نسخه متنی

Neil Roodyn

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید












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]

3.

Now we can work on adding the tags to this XML file that we need to be able to build the project using NAnt. We'll start by defining the project in the build file, as shown. The project element is attributed with name and basedir; these represent the project name and the base directory for the build process, respectively.


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.


<?xml version="1.0" encoding="utf-8" ?>
<project name="NAntTest" basedir=".">
</project>

4.

Next we will add some properties to the project; these will be used by the other sections in the build file.


<project name="NAntTest" basedir=".">
<property name="basename" value="NAntTest"/>
<property name="debug" value="true"/>
<property name="build.dir" value="build"/>
</project>

5.

We can now define a target, nested within the project, for the build, as shown here.


<?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.

    The sources element, which is a child of the csc element, defines the source files to compile for this target. Notice here we are including all C# files.

    The references element enables us to define all the assemblies we need to reference for the build of this target to work.

    The final child element of the csc element is arg; this enables us to define free-form text arguments to pass to the compiler. Here we are defining the main assembly entry point.


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]

8.

We can now define some more targets within the project. This enables us to carry out more than one type of build from the same file. Let's start by defining a target that actually doesn't build at all but instead deletes all the build output. We create a new target called clean, as shown here.


<target name="clean"
description="deletes the build directory">
<delete dir="${build.dir}" verbose="true"
failonerror="false"/>
</target>

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).


Figure 7-4. Run NAnt using the clean target.



[View full size image]

10.

We can go back to the outermost project element and add an attribute to indicate the default target, as shown; this will set the default target to our build target. You can test this by just calling NAnt from the command line without specifying a target to build.


<project name="NAntTest" default="build" basedir=".">

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.


<target name="debug" depends="clean">
<property name="debug" value="true"/>
</target>
<target name="release" depends="clean">
<property name="debug" value="false"/>
</target>

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.


Figure 7-5. NAnt builds the release and build targets.



[View full size image]

13.

Finally, we are going to add a target that will execute the program that we have built. The exec element contains the program attribute to define the program to execute and the basedir attribute to define where to execute the program.


<target name="run" depends="build">
<exec program="${build.dir}/${basename}.exe" />
</target>

14.

We can now build and run the program using NAnt (see Figure 7-6).


Figure 7-6. Build and run the project from NAnt.



[View full size image]

15.

You can also choose whether to build and run the debug or the release version, as shown in Figure 7-7.


Figure 7-7. Use NAnt to do a release build and run the application.



[View full size image]

16.

With the knowledge you have gained from this exercise, you should be able to add some NUnit tests to the project and then create a target that builds the project and runs the tests.

You now have the knowledge you need to automate your build using batch files and NAnt, and should be able to choose which would suit you best and set up an automated build and test process for your projects. If you can get access to another machine, I strongly recommend setting up an integration machine to carry out continuous build and test cycles.

To exercise your newly gained skills, take the project you are working on at the moment and set up an automated build for it. Even if it is not a .NET project, you can do this in a batch file or use the Windows Scripting Host.

MSBuild

MSBuild is a new build tool from Microsoft. MSBuild is Microsoft's version of NAnt. MSBuild files are XML scripts similar to the NAnt script we used in Exercise 7-2. Visual Studio.NET 2005 will automatically generate this script for each project you create from the IDE. What's more, the IDE will use this MSBuild script to compile and build the project. When you press F5, the MSBuild script will run.

You can edit the script the same way you edit a NAnt script. You can add steps to the build process, and then when you press F5 from the IDE those steps will be executed. This means that getting started with setting up automated builds will become much easier.

MSBuild has an extensible architecture through the use of a new ITask interface in the .NET Framework. If you build a class that supports the ITask interface, MSBuild can execute functionality in that class as part of your build process.

The .NET Framework version 2.0 will include MSBuild. Let me say that again: MSBuild will ship with the .NET Framework, not just future versions of Visual Studio.NET. The fact it ships with the framework is important. It means you can build your product without having Visual Studio.NET installed. Making the build process rely only on having the .NET Framework means that you can use any managed language or development environment of your choice and still have the same build process.

    / 117