FXCop is great for checking code, and it's easy enough to run the command
line utility from MSBuild. This will run the analysis and produce a report
of all the issues, but this has no "teeth", that is, nothing makes anyone
look at the report and fix the issues. In order to have FXCop actually
break the build you need to write a custom task the following is an example
of a simple task that breaks the build if there are any issues. It does
need to be told where to find the report via the report location property
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Xml;
namespace Framework.Build.CustomTasks
{
public class CheckFXCop : Task
{
private string _reportLocation;
[Required]
public string ReportLocation
{
get { return _reportLocation; }
set { _reportLocation = value; }
}
/// <summary>
/// Executes the Task
/// </summary>
/// <returns>true if the task successfully executed; otherwise,
false.</returns>
public override bool Execute()
{
bool returnVal = true;
int numIssues = 0;
try
{
FileStream xmlFileStream = new
FileStream(this.ReportLocation, FileMode.Open);
XmlTextReader fxReport = new XmlTextReader(xmlFileStream);
while (fxReport.Read())
{
if (fxReport.LocalName == "Issue")
{
returnVal = false;
numIssues++;
}
}
if (!returnVal)
{
Log.LogError("There are " + numIssues.ToString() + "
FXCop issue(s). Please review the report");
}
return returnVal;
}
catch
{
return false;
}
}
}
}