Monday, 29 December 2008

NAnt HowTo #1: How To Compile A Project?

NAnt provides a CSC task that will allow you to compile your C# project (sorry, VB guys, no info for you :)). It may be used this way:

<!--  2. Building shared data project -->
target="library" debug="false" warnaserror="true"
name="**/*.cs" />

name="D:/projects/MyProject/External/NHibernate.dll" />


number="1702" />

So let's have a look in details at each part of this task.


One by one:
  1. target="library" - indicates that we want to have a .dll as result of our compilation. Possible values are exe, winexe, library or module.

  2. debug="false" - indicates that no debug symbols will be included into our assembly. Possible values are Enable, Full, None and PdbOnly. Although you can use aliases (like I did): true stands for Enable and false stands for None.

  3. warnaserror="true" - has same effect like checking 'Treat warnings as errors - All' option in Visual Studio. All warnings will be treated as errors.

  4. output="D:/projects/MyProject/Integration/MyProject.dll" - output of compilation. Make sure your output target extension is adequate to your compilation target (although I haven't tried compiling target library to output .exe :-P)


This nested element allows you to select files that should be compiled. It's only attribute I use here is:

="D:/projects/MyProject/Source" - base directory for source files. Usually this is root folder for some project.

The <include> element allows you to specify elements that should be compiled. It's attribute, name, deserves some attention. First, it uses wildcards to pick necessary items. Second, it uses double asterisk to recursively pick all files from all folders. Have a look:
  1. name="*.cs" will pick .cs files only in current folder

  2. name="**/*.cs" will pick .cs files from current folder and from all subfolders, their subfolders, etc.


This nested element allows you to specify assemblies that should be references. Syntax also supports wildcards so you can easily specify *.dll to reference all .dll files in some folder.


This element allows you to deal with resources embedded into your assembly. In this particular case I use this element to embed NHibernate .hbm mappings.


This section allows you to ignore some specific warnings via nested <warning /> element. Use it's number="1702" attribute to specify some specific warning you want to ignore.


That's a short explanation on CSC target, more information can be found here. Hope this post will be helpful to anyone other than myself :D

kick it on

No comments: