FxCop is an easy to use static analysis tool, which scans .NET assemblies for violations to pre-defined rules (like naming conventions, security, etc)– while it is now bundled with VSTS, there are still plenty of NAnt-loving shops out there. If your .NET project uses NAnt as a build system and you’re looking for a quick mechanism to objectively look at your code base, then follow these four easy steps for FxCop-NAnt nirvana.
First, you must download NAntContrib, which is a series of tasks not yet included in the core NAnt distribution. Inside this .dll are a series of tasks including fxcop, which of course, runs FxCop (assuming FxCopCmd.exe is in your PATH). The NAntContrib project, by the way, has a host of tasks for working with various CM systems like Perforce, Subversion, and ClearCase to name a few.
Next, download FxCop, install it, and ensure that FxCopCmd.exe is in your PATH. Installing FxCop provides a rather nice GUI that can be run outside of a particular build system too. For example, below is an example of the FxCop application’s results when run against a particular .dll.

Now that you’ve downloaded both NAntContrib and FxCop, the next step is to add a new target to your NAnt build. There are a host of options associated with the fxcop task, however, by far the easiest (and most flexible) is something like this:
<target name="fxcop" depends="build">
<fxcop>
<targets>
<include name="${build.dir}bin${build.config}${release.dll}" />
</targets>
<arg value="/out:${build.dir}bin${build.config}fxcop.xml" />
</fxcop>
</target>
Note that this task depends on a binary being in place, hence the depends clause specifies that build be run first. Also note that I’ve specified that FxCop output its results to an XML file, which I can now run my own XSL or one of the included style sheets in the FxCop distribution.
Now that everything is in place, the last step for achieving FxCop-NAnt nirvana is to run it, of course!

April 10th, 2007 at 1:36 pm
[…] ebook wrote an interesting post today onHere’s a quick excerptFxCop is an easy to use static analysis tool, which scans .NET assemblies for violations to pre-defined rules (like naming conventions, security, etc)– while it is now bundled with VSTS, there are still plenty of NAnt-loving shops out … […]
May 3rd, 2007 at 6:07 am
Folks interested in FxCop might be interested by the tool NDepend:
http://www.NDepend.com
NDepend analyses source code and .NET assemblies. It allows controlling the complexity, the internal dependencies and the quality of .NET code.
NDepend provides a language (CQL Code Query Language) dedicated to query and constraint a codebase.
It also comes from with advanced code visualization (Dependencies Matrix, Metric treemap, Box and Arrows graph…), more than 60 metrics, facilities to generate reports and to be integrated with mainstream build technologies and development tools.
NDepend also allows to compare precisely different versions of your codebase.
August 18th, 2007 at 11:32 am
[…] Test Early ยป 4 steps to FxCop-NAnt nirvana (tags: nant .net) […]
April 1st, 2008 at 3:22 am
ugh, but I find fxcop does not fail the build which mostly renders it useless in team development. People are way more motivated to correct mistakes when it breaks the build and with the autobuilds running, it sends out email right after someone breaks it to the team which encourages them to fix it, but I can’t seem to get fxcop to fail the build. Does your build fail or just throw out a bunch of stuff to the console which is annoying???? I like java’s pmd and checkstyle better where I can fail the build and eliminate rules I don’t like from failing the build.