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 -->
<csc
target="library" debug="false" warnaserror="true"
output="D:/projects/MyProject/Integration/MyProject.dll">
<sources
basedir="D:/projects/MyProject/Source">
<include
name="**/*.cs" />
</sources>

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

<resources>
<include
name="D:/projects/MyProject/Source/NHib/*.hbm.xml"/>
</resources>

<nowarn>
<warning
number="1702" />
</nowarn>
</csc>

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

Attributes

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)

Sources

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

basedir
="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.

References

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.

Resources

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.

Nowarn

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.

Conclusion


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 DotNetKicks.com

No comments: