NAnt build file tips

I found the documentation for NAnt to be lacking in build file examples, especially examples that deal with more than one task or include. If you’re completely new to NAnt, I’d suggest reading the article that Jeffrey McManus wrote for ondotnet. If not, read on.

One of the first things you’ll notice when reading examples on any site is that most of them are explicit in regard to the fileset to be compiled. You’ll see this:

<csc target="library" output="company.dll">
  <includes name="src\com\company\packagea\ClassA.cs"/>
  <includes name="src\com\company\packagea\ClassB.cs"/>
  <includes name="src\com\company\packageb\ClassC.cs"/>

instead of something like this:

<csc target="library" output="company.dll">
  <includes name="src\com\company\packagea\*.cs"/>
  <includes name="src\com\company\packageb\*.cs"/>

Of course, this means that you have to manually add in each namespace, which can be tedious and error prone. NAnt provides a shorthand: **, which means we can reduce the above to this:

<csc target="library" output="company.dll">
  <includes name="src\com\company\**\*.cs"/>

If you don’t want a particular file included in the list (say for instance all the files in ‘packageb’), you can use the <excludes> tag like so:

<csc target="library" output="company.dll">
  <includes name="src\com\company\**\*.cs"/>
  <excludes name="src\com\company\packageb\*.cs"/>

That’s all I had off the top of my head. There are a couple new features in release .84 that might be worth taking a look at: xmlpeek (which allows you to extract text from an xml file like say for instance the web.config), xmlpoke (which allows you to replace text in an xml file…) and servicecontroller (which allows you to stop/start/restart Windows Services).

12 thoughts on “NAnt build file tips”

  1. I was going thro’ your article on NANT tips. Can you suggest me tips in the
    following scenarios?

    1. <includes asis=”true”
    name=”..\..\..\..\..\Microsoft.NET\Framework\v1.1.4322\System.dll” />
    <includes asis=”true”
    name=”..\..\..\..\..\Microsoft.NET\Framework\v1.1.4322\System.Data.dll” />
    <includes asis=”true”
    name=”..\..\..\..\..\Microsoft.NET\Framework\v1.1.4322\System.XML.dll” />

    Is there a way to combine the above three statements into one?

    <includes name=”CGS\*.aspx.cs” />
    <includes name=”CGS\*.cs” />
    <includes name=”CGS\Controller\*.cs” />
    <includes name=”CGS\Interface\*.cs” />

    Is there a way to combine the above four into one?

    Any help of this would be great. Thanks for your time.


  2. — On #1, not really, although you could use the pattern matching capability of Nant to include all the ‘..\..\..\..\..\Microsoft.NET\Framework\v1.1.4322\System*.dll’ but that would include a bunch of other dll’s that you probably don’t want (like System.Security.dll and System.Messaging.dll)

    Re: #2 you could use something like this:

    <includes name=”CGS\*.cs” />
    <includes name=”CGS\**\*.cs” />

    The first includes statement should grab any and all files ending .cs in the CGS directory and the second includes will grab any and all files ending in .cs in any subdirectory of CGS.

  3. I have a web solution that consists of 13-18 projects, each having a lot of *.cs/*.vb files. Other necessary files are also there. Also all the projects are interdependent. Can you tell me how to easily make a build of this solution using NAnt. Any help would be highly appreciated. I am pretty new to NAnt.

  4. Vishal: I’ve never worked on a project with both .vb and .cs files, so I don’t have any direct experience doing what you’ve been tasked with. However, it seems to me that you would have multiple <target> elements, maybe one target that built the *.cs files and one that build the *.vb files and then determine which projects depend on what… build project a, b, and c first and then make the results available for the other 15 projects.

  5. Can you please tell me how to include Web References in a build file. I am able to include dll References in my build files, but now got stuck in adding Web References. I will be highly thankful to you.

  6. hi Vishal,

    Sorry it’s taken so long to get back you. I’m guessing that you’re talking about web references (, which means you’re talking about referencing a web service from NANT? I think (and I could be totally wrong here) that you only need to add a reference to a web service if you’re using it directly from Visual Studio. Also guessing, but I bet that when you do add a web reference to Visual Studio that Visual Studio then generates the web service stubs for you; you can just as easily generate the web service stubs using wsdl.exe and then you’ll have nice .cs source files that you can use instead. That help?


  7. I have problem with parameters in nant deployment processes..
    I want to specify files that will be deployed in a flat file. but ı do not know how to handle with it in nant thanks.

  8. I created a simple c# project, with a class called TestClass.cs and this class contains couple of interfaces with out implementing any functionality.

    Now, when I compile this project in VS.Net, it generated me a 16KB dll file called TestProject.dll.

    Going further, I put a Nant build script and use CSC to complete the same TestClass.cs file. This generated me a 4KB dll file called TestProject.dll

    I am not able to understand what is that is putting into the DLL file which is 4 times the size of the dll file generated with nant script.

    Can somebody help me out in understanding and solving this problem?

    Note : The above example is an example used from my actual project. In real time the page crashes with the problem saying the error message as “Value cannot be null. Parameter name: stream”

    Please do help me out, if any body knows the solution. ASAP

  9. Hello i am junior Configuration Manager recently joined. I wanted to know what is a Build and how it is done step by step process please help me out guys

  10. Thanks for this article. I think it will help me. Not only the article itself, but the comments, questions and answers are helpful too. Keep up the good work.


Leave a Reply

Your email address will not be published. Required fields are marked *