Introduction

Nowadays nightly builds are the most important entity for any project development lifecycle. NAnt is one of the most popular tools for .NET automated builds. NAnt is the same as Ant but it is specially designed for .NET.

This article will explain the procedure for writing the build file for an ASP.NET application in brief. I am expecting that the reader knows the basics of XML technology and deployment of an ASP.NET application.

Background

The deployment of an ASP.NET application is little bit tricky. When you build the ASP.NET application it generates a separate DLL file for each class library included in the project. However, ASP.NET 2.0 does not generate a DLL file for your code behind files, i.e. ".cs" in case of C#. We have to use aspnet_compiler.exe, which comes along with the .NET 2.0 Framework for generating DLL files for CS files. Again it generates a separate DLL for each CS. All this makes build and deployment processes very complicated.

Here I am trying to guide you on how to write a build file for ASP.NET's application automated build using NAnt. NAnt is a very popular build tool. It's open source and you can download it freely from SourceForge. To use this code, you have to download some external tools to setup your dev environment.

Using the Code

As mentioned earlier, you have to first install "Visual Studio 2005 Web Deployment Projects." Just download it from the MSDN site and install it on a default location. This installation is required to merge multiple DLL files generated by MSBuild.exe.

Download NAnt and copy the folder in to C: (you can copy it into any directory; I am giving C: just as an example and I have used the same path in the build file given below). Now go to the C:\nant\nant-0.85\bin folder. Here you can see a set of some XML/XSL files and one "lib" folder.

Create one new file here and name it as build.number.xml. This file will specify the current version of the project in the source control. Copy the following contents and save the file.

Now you are ready to write an actual build file. By default, the nant folder contains one file: Nant.build. This is a simple XML file where you can write your build code. You can use a separate file for your build code with the extension ".build." You can run this build code using nant.exe. There are several options available with this EXE file. You can read it from NAnt documentation available here.

I am not giving NAnt coding basics here. You can read it from NAnt's help. So let's start writing the heartbeats for your automated build file. Open the NAnt.build file in any text or XML editor. First, create the rootnode for which you will specify the name of the project and default target to which NAnt.exe will execute to start the build process.

Between the project tag, we have to write some targets and properties. To make our code more generic and configurable, we have to declare some properties first. Please see code comments for description.

<!--<span class="code-comment">Properties for build process--></span><!--<span class="code-comment"> BuildDir is the directory where our .NET source
resides.Project directory for web application--></span><propertyname="BuildDir"value="D:\Work"/><!--<span class="code-comment"> target is the directory where we are going to
store deployable files.--></span><propertyname="target"value="D:\Work\deploy"overwrite="false"/><!--<span class="code-comment"> SolutionFileName is the name of your solution file.--></span><propertyname="SolutionFileName"value="NantTrial.sln"/><!--<span class="code-comment"> BuildNumberPath is path of the xml file where your
current build no is specified.--></span><propertyname="BuildNumberPath"value="C:\nant\nant-0.85\bin\build.number.xml"/><!--<span class="code-comment"> dotnet is the path of the folder where .NET
framework is installed.--></span><propertyname="dotnet"value="c:/windows/Microsoft.NET/Framework/v2.0.50727"overwrite="false"/><!--<span class="code-comment"> MSBuildPath is the path for the MSBuild.exe. This file
comes along with .NET framework and
being stored at the same folder.--></span><propertyname="MSBuildPath"value="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe"/><!--<span class="code-comment"> WebDeploymentExePath is the path of the folder
where you have installed WebDeployment Project.--></span><propertyname="WebDeploymentExePath"value="C:\Program Files\MSBuild\Microsoft\WebDeployment\v8.0"/><!--<span class="code-comment"> WebProjectFolderName is your project's directory name--></span><propertyname="WebProjectFolderName"value="NantTrial"/><!--<span class="code-comment"> FinalDeployDllName is name which you want to give your
final .dll file generated by WebDeployment tool.--></span><propertyname="FinalDeployDllName"value="ProjectName"/><!--<span class="code-comment"> newReleaseLabel can be used for naming the folder which
contains final deployable files or labelling the source in
source control.--></span><propertyname="newReleaseLabel"value="ProjectName-0-0-0-0"/><!--<span class="code-comment">Major build number--></span><propertyname="build.version.major"value="0"/><!--<span class="code-comment">Minor build number--></span><propertyname="build.version.minor"value="0"/><!--<span class="code-comment">build number--></span><propertyname="build.version.build"value="0"/><!--<span class="code-comment">revision number--></span><propertyname="build.version.revision"value="0"/><!--<span class="code-comment">Ending Properties for build process--></span>

You can configure the above properties as per your requirements. There are a total of four targets in the build file.

buildProject [Default target responsible for building the .NET application and creating DLL files in the deploy folder]

clean [Remove the previous deploy folder]

setversion [Set version string in application's AssemblyInfo.cs file]

setVersionString [Set version string property, which can be used for labelling the source or naming the deply folder]

incrementBuildNumber [This will execute XSL file to increment current version by 1]

All these properties depend on each other and will be executed in the bottom-up manner (5 to 1). You can configure this as per your requirements using the target's "depends" property. You can download the attached sample NAnt.build file for the above targets. The key part of the article lies in the buildProject target. Here we are going to build the web application.

To build the application, we have to use .NET's command line utility MSBuild.exe. This file lies in the .NET Framework's folder by default. This file takes the solution file name as an argument. Execute it using following code.

Now the code can be compiled using aspnet_compiler.exe. This file also comes along with the .NET Framework installation and resides in the same folder. aspnet_compiler.exe has multiple command line options. You can use it based on your needs. The complete set of options are described here. Execute aspnet_compiler.exe using following code.

This tool will generate a separate DLL for your class libraries, but the problem is that it also generates separate DLLs for every CS file. It's not a good idea to ship your build with these multiple DLL files and have one for each web page. So, there is need to merge these multiple DLL files into one final deployable DLL.

You can do this using Visual Studio 2005 Web Deployment Projects. Installation of Web Deployment Projects copies aspnet_merge.exe to your build machine. It takes the target folder name and name for the final DLL as an arguments. Execute aspnet_merge.exe using following code.

Now open the command prompt. Change the directory to your NAnt folder (i.e. C:\nant\nant-0.85\bin\ in my case). Type Nant.exe and press "Enter." The build process will start. You can see the verbose on the command line. If the build succeds, it will give the following message:

BUILD SUCCEEDED
Total time: 33.3 seconds.

Now check your "target" folder, i.e. the value of the property "target" in the build file. In my case it is D:\Work\deploy. All web pages (ASPX) should be there with one bin folder and the precompiledApp.config file. The bin folder should contain all DLLs for class libraries and one DLL for your CS file instead of seperate DLLs for each. The name of the DLL should be the value of property FinalDeployDllName. In my case it is ProjectName.

Just rock!! You are now ready for deployment. The main advantage of NAnt automated builds is that you can schdule your nightly builds. Just create one BAT file with the command NAnt.exe and schedule its running time using Windows scheduler.

Points of Interest

While designing an ASP.NET application one should keep in mind not to keep your solution file in the same folder where your web pages are being stored. MSBuild.exe will fail to perform build in this case.

I am planning to write a separate article for NAnt's common targets like source control tasks, send mail, copy files to folders on server, etc. I will start them shortly.

The versioning targets are being referred from here. Go through this nice blog on versioning using NAnt.

Hello Vinayak..
Nice post.
I have a problem building Xamarin Android project using NAnt and cc.net. I did not get .apk file when I automate the build. I have set property to release i.e. configuration:release.
Please help me out...

Hi Vinayak,
Thanks for your article.
I have followed the steps.But iam getting error in aspnet_merge.exe
[exec] error 1013: Cannot find any assemblies that can be merged in the app
lication bin folder.

Your posts are in the 'Post Message' section
Did you get a solution for your problem. Were you able to create a add-in button on lotus notes toolbar. If yes, can you give me a hint on how to go about it.

I am sorry for posting this message which is irrelevant to this topic but this was the only way I could contact you. I am searching for days on this one and haven't been able to find anything. Any help would be appreciated.

Hai,
I execute the script given by u, and it also successfully build but there is no dll generated in the bin folder. only the bin folder and precompiledapp.config were generated in the target place.
Pls advice me.

First thanks for ur quick responce.I have checked the path given in the property name for deploy, thats clear, even the bin folder and PrecompiledApp.config file also generated in that given path but there is no dll generated in that bin.

Yes, I forgot to attach and now I am not sure how I can attach it again. Post-in your mail id so I can email you the script.
For versioning there is one nice blog on web. Please go through it. I had referred the same.
blog:
http://bloggingabout.net/blogs/joshua/archive/2005/07/26/8675.aspx

Thanks! I think while formatting an article editors forgot to attach the build file. Anyways you can write it at your own. Just copy paste all targets and properties in the same sequence between <project></project> tags. Let me know in case of any issues.