I've been working on something similar. Even though this question is a bit old, I'm hoping it will help you.
I started out like most -- by making a post-build event that calls FxCopCmd.
In my case, I wanted just a small subset of the code, some of the built-in rules, and also some custom rules (in a .dll)
I used an .fxcop project file for this -- configuring it all just how I wanted via the GUI and then pointing FxCopCmd to the project file in the post-build event.
For the most part, it worked great, but the rule violations came up only as warnings. The "Treat warnings as errors" option doesn't seem to apply to this, so I had to come up with a different solution.
What ultimately worked best for me was based on a blog post I stumbled upon.
I modified the project file to add in two new events.
I have a few extra parameters and stuff for FxCop, but the gist of it is:
1: <PropertyGroup>
2: <FxCopResults>$(ProjectDir)obj\$(Configuration)\FxCopResults.xml</FxCopResults>
3: <PostBuildEvent>"%25ProgramFiles%25\Microsoft FxCop 10.0\FxCopCmd.exe" /file:"$(TargetPath)" /console /out:"$(ProjectDir)obj\$(ConfigurationName)\FxCopResults.xml"</PostBuildEvent>
4: </PropertyGroup>
5: <Target Name="BeforeBuild">
6: <Delete Files="$(FxCopResults)" ContinueOnError="true" />
7: </Target>
8: <Target Name="AfterBuild">
9: <Error Text="One or more FxCop warnings occurred." Condition="Exists('$(FxCopResults)')" />
10: </Target>
The general flow is like this:
- (BUILD PROCESS IS TRIGGERED)
- Before a build starts, the previous FxCop results (if they exist) are cleared out.
- Pre-Build Event is triggered
- (BUILD BEGINS)
- Post-Build Event is triggered (which runs FxCopCmd)
- After the Post-Build finishes, if there are FxCop results, an error is raised.
- (BUILD PROCESS IS COMPLETE)
Now, if the FxCop analysis generated -- for example -- 4 rule violations, your build would generate 4 warnings and 1 error.
I hope this helps.