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.

fxcop-con

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!