This post shows two options for getting FxCop 1.36 running on a each Visual Studio 2008 build without maintaining an FxCop project separate from a C# project. On the downside it will run the same set of rules for each project, which works for my scenario but may pose problems when working with legacy code and new development because the legacy code will probably violate many rules.
This instructions assume the following environment:
- MSbuild Version 3.5.30729.4926
- 64 bit Windows (otherwise you will need to modify the C:\Program Files (x86) paths )
- Visual Studio 2008
- FxCop 1.36
- Install FxCop
- Create a file: C:\Program Files (x86)\MSBuild\v3.5\FxCop.targets with the following text:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup><!-- Add FxCop to the list of targets to build --><BuildDependsOn>$(BuildDependsOn);FxCop</BuildDependsOn></PropertyGroup><!-- Define the FxCop target we added above.--><Target Name="FxCop"><Message Text="$(MSBuildToolsPath)" /><!-- The Condition attribute allows others to use the same project file without breaking their build when FxCop is not installed--><Exec Command=""$(ProgramFiles)\Microsoft FxCop 1.36\FxCopCmd.exe" /file:"$(TargetPath)" /console"Condition="Exists('$(ProgramFiles)\Microsoft FxCop 1.36\FxCopCmd.exe')" /><!-- When running on a 64 bit machine the $(ProgramFiles) variable will be set to c:\program files (x86) when running a 32 bit process which luckily for us Visual Studio and FxCop are.--></Target></Project> - Open the Visual Studio
- File --> New Project (ctrl+shift+n)
- Select Other Project Types --> Visual Studio Solutions --> Blank Solution
- Name the solution FxCopIntegrated and press OK
- In Solution Explorer (ctrl +alt + L) right click the solution and select add new project.
- Select Visual C# --> Windows --> Class Library
- Name the library CodeBandit and press OK.
- In Solution Explorer right click the CodeBandit project and click unload project.
- If Visual Studio prompts to save files click "yes"
- In Solution Explorer right click the CodeBandit project and click "Edit CodeBandit.csproj"
- Navigate to the line after this (ctrl+g 52 on my machine):
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> - Add this line to reference the file created in step 2:
<Import Project="$(MSBuildExtensionsPath)\v3.5\FxCop.targets"/> - In Solution Explorer right click the CodeBandit project and click reload project.
- If Visual Studio asks to close CodeBandit.csproj click "yes" and "yes" to save changes if prompted.
- Build the solution. On my system I received these warnings:
CA2210 : Microsoft.Design : Sign 'CodeBandit.dll' with a strong name key.
CA1014 : Microsoft.Design : Mark 'CodeBandit.dll' with CLSCompliant(true) because it exposes externally visible types.
- Follow the steps from Option #1 to ensure everything is setup and working.
- Create a file: C:\Program Files (x86)\MSBuild\v3.5\Custom.After.Microsoft.Common.targets with the following text:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="FxCop.Targets"/></Project> - Thats it, all the work was performed in Option #1 :)
- To verify this add a new VB project to the FxCopIntegrated solution.
- Building the newly added project I get these warnings:
CA1020 : Microsoft.Design : Consider merging the types defined in 'ClassLibrary2' with another namespace.
CA2210 : Microsoft.Design : Sign 'ClassLibrary2.dll' with a strong name key.
CA1014 : Microsoft.Design : Mark 'ClassLibrary2.dll' with CLSCompliant(true) because it exposes externally visible types.
CA1824 : Microsoft.Performance : Because assembly 'ClassLibrary2.dll' contains a ResX-based resource file, mark it with the NeutralResourcesLanguage attribute, specifying the language of the resources within the assembly. This could improve lookup performance the first time a resource is retrieved.