Click here to Skip to main content
15,794,236 members
Articles / Web Development / ASP.NET

ASP.NET Web Application's Build Using NAnt

Rate me:
Please Sign up or sign in to vote.
3.57/5 (4 votes)
13 Sep 2007CPOL6 min read 103.4K   787   51   21
This article explains how to use NAnt for ASP.NET web applications' automated build.


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.


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.

  1. NAnt
  2. Visual Studio 2005 Web Deployment Projects

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.

<?xml version="1.0" encoding="utf-8"?>
    <copyright>Copyright 2007</copyright>

The schema of the above file should be in the same folder. Create one file and name it as genVer.xsl. Copy the following code and save the file.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
    <xsl:output method="text"/>
    <xsl:template match="/">

#define VER    <xsl:value-of select="build/major"/>,
               <xsl:value-of select="build/minor"/>,
               <xsl:value-of select="build/build"/>,
               <xsl:value-of select="build/release"/>

#define STRVER     "<xsl:value-of select="build/major"/>,
                    <xsl:value-of select="build/minor"/>,
                    <xsl:value-of select="build/build"/>,
                    <xsl:value-of select="build/release"/>"

#define COMPANYNAME "<xsl:value-of select="build/companyname"/>"

#define COPYRIGHT "<xsl:value-of select="build/copyright"/>"

Now we have to do some XSL coding which will help us to increment the version number. So create one new file in the same folder and name it as IncrBuild.xsl. Copy the following code and save the file.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/build/build">
        <xsl:variable name="buildNumber" select="."/>
        <xsl:element name="build">
            <xsl:value-of select="$buildNumber + 1"/>

    <xsl:template match="/ | @* | node()">
          <xsl:apply-templates select="@* | node()"/>

Now you are ready to write an actual build file. By default, the nant folder contains one file: 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 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.

<project name="ProjectName" default="buildProject" 

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.

<!--Properties for build process-->
    <!-- BuildDir is the directory where our .NET source 
        resides.Project directory for web application-->
    <property name="BuildDir" value="D:\Work"/> 
    <!-- target is the directory where we are going to 
        store deployable files.-->
    <property name="target" value="D:\Work\deploy" overwrite="false" />
    <!-- SolutionFileName is the name of your solution file.-->
    <property name="SolutionFileName" value="NantTrial.sln"/>

    <!-- BuildNumberPath is path of the xml file where your 
        current build no is specified.-->
    <property name="BuildNumberPath" 
    <!-- dotnet is the path of the folder where .NET 
        framework is installed.-->
    <property name="dotnet" 
    <!-- MSBuildPath is the path for the MSBuild.exe. This file  
        comes along with .NET framework and 
        being stored at the same folder.-->
    <property name="MSBuildPath" 
        value="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe" />
    <!-- WebDeploymentExePath is the path of the folder 
        where you have installed WebDeployment Project.-->
    <property name="WebDeploymentExePath" 
        value="C:\Program Files\MSBuild\Microsoft\WebDeployment\v8.0" />
    <!-- WebProjectFolderName is your project's directory name-->

    <property name="WebProjectFolderName" value="NantTrial" />
    <!-- FinalDeployDllName is name which you want to give your 
        final .dll file generated by WebDeployment tool.-->
    <property name="FinalDeployDllName" value="ProjectName" />
    <!-- newReleaseLabel can be used for naming the folder which 
        contains final deployable files or labelling the source in 
        source control.-->
    <property name="newReleaseLabel" value="ProjectName-0-0-0-0"/>
    <!--Major build number-->
    <property name="build.version.major" value="0"/>    
    <!--Minor build number-->
    <property name="build.version.minor" value="0"/>    
    <!--build number-->

    <property name="" value="0"/>    
    <!--revision number-->
    <property name="build.version.revision" value="0"/>    
    <!--Ending Properties for build process-->

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

  1. buildProject [Default target responsible for building the .NET application and creating DLL files in the deploy folder]
  2. clean [Remove the previous deploy folder]
  3. setversion [Set version string in application's AssemblyInfo.cs file]
  4. setVersionString [Set version string property, which can be used for labelling the source or naming the deply folder]
  5. 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 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.

<exec program="${MSBuildPath}">
    <arg value="${BuildDir}\${SolutionFileName}" />

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.

<exec program="${dotnet}/aspnet_compiler.exe" 
    "-f -u -p ${BuildDir}\${WebProjectFolderName} -v / ${target}"/>

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.

<exec program="${WebDeploymentExePath}\aspnet_merge.exe" 
    commandline="${target} -o ${FinalDeployDllName} -xmldocs"/>

This whole process will copy some unnecessary files to target the deployment folder, which we need to remove. Remove these files using:

    <!--Remove unnecessary files from the build.-->
            <include name="${target}/*.build" />
            <include name="${target}/*.scc" />
            <include name="${target}/*.sln" />
            <include name="${target}/build.*" />

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:


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer Covelix Technologies, Pune, India.
India India
5+ years of technical experience.

Completed graduation from Govt. Engineering college, Pune.
Working as SW developer in Covelix for more than 3 years.

My technical experience includes n-tier applications in .NET, MFC | C, C++, C#, Python | SQL server, Oracle 10g | AJAX | Installsheld.

Comments and Discussions

BugDoubt Pin
priyank,:)13-Aug-15 22:32
priyank,:)13-Aug-15 22:32 
QuestionXamarin Android Project Pin
d87manish5-Jun-13 1:15
d87manish5-Jun-13 1:15 
Generalabout Unresolved assembly reference not allowed: System.Web Pin
Member 922815811-Sep-12 0:21
Member 922815811-Sep-12 0:21 
GeneralMy vote of 5 Pin
v.sundaramoorthy11-Jan-12 2:35
v.sundaramoorthy11-Jan-12 2:35 
QuestionNAnt+aspnet_merge.exe Pin
gnanisa23-Nov-11 20:39
gnanisa23-Nov-11 20:39 
QuestionRe: NAnt+aspnet_merge.exe Pin
Member 1225914520-Jan-16 2:03
Member 1225914520-Jan-16 2:03 
QuestionHow to build with Xalan.? Pin
BooWangno6-Aug-10 1:12
BooWangno6-Aug-10 1:12 
GeneralNice article Pin
Dan Cao16-Nov-09 17:34
Dan Cao16-Nov-09 17:34 
QuestionHow to change default location of dlls using nant Pin
vamsiscorpio1-Jun-09 2:14
vamsiscorpio1-Jun-09 2:14 
GeneralRegarding Lotus Notes Pin
Member 204045917-Jun-08 4:14
Member 204045917-Jun-08 4:14 
GeneralRe: Regarding Lotus Notes Pin
VinayakChitre17-Jun-08 6:27
VinayakChitre17-Jun-08 6:27 
QuestionHow to refer the dll Pin
Rajeshv28-Apr-08 0:59
Rajeshv28-Apr-08 0:59 
Questionno dll generated in bin Pin
Rajeshv22-Apr-08 3:21
Rajeshv22-Apr-08 3:21 
GeneralRe: no dll generated in bin Pin
VinayakChitre22-Apr-08 3:35
VinayakChitre22-Apr-08 3:35 
GeneralRe: no dll generated in bin Pin
Rajeshv24-Apr-08 4:10
Rajeshv24-Apr-08 4:10 
GeneralUnable to Understand Version Info part Pin
Member 367266014-Apr-08 14:46
Member 367266014-Apr-08 14:46 
GeneralRe: Unable to Understand Version Info part Pin
VinayakChitre14-Apr-08 20:14
VinayakChitre14-Apr-08 20:14 
QuestionI love it! But where's "The attached file"? Pin
indomitablehef31-Oct-07 2:40
indomitablehef31-Oct-07 2:40 
AnswerRe: I love it! But where's &quot;The attached file&quot;? Pin
VinayakChitre31-Oct-07 4:31
VinayakChitre31-Oct-07 4:31 
Generalway too complicated... Pin
Thanks for all the fish14-Sep-07 4:35
Thanks for all the fish14-Sep-07 4:35 
GeneralRe: way too complicated... Pin
snkscore17-Sep-07 13:14
snkscore17-Sep-07 13:14 
You can use NAnt to lanuch with a command line argument pointing to the SLN file.

Devenv will use the SLN file to build the entire solution.

It isn't perfect, but it works great for me.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.