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">
<sources>
<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">
<sources>
<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">
<sources>
<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">
<sources>
<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).
11 Comments