FAQ: How do I share Managed Code Analysis rule settings over multiple projects? [David Kean]
If your team has a minbar of Managed Code Analysis rules that must be explicitly fixed or suppressed, it is possible to share the Managed Code Analysis rule settings over multiple MSBuild projects (.csproj, .vbproj).
To share the minbar between multiple projects, do the following:
Using Visual Studio, create a new empty project
In Solution Explorer, right-click the project and choose Properties
In the Project Properties window, choose the Code Analysis tab
In the Code Analysis tab, choose the rules you want for your minbar
In Solution Explorer, right-click the project and choose Unload Project, answering Yes to any prompt to save changes
In Solution Explorer, right-click the project and choose Edit
Search for the <CodeAnalysisRules> element and copy the text within it
Create an empty text file called [team].CodeAnalysis.Rules.targets, replacing [team] with the name of your team
In the text file enter the following, replacing [rulesettings] with the text copied above in Step 6:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <CodeAnalysisRules>$(CodeAnalysisRules); [rulesettings]< /CodeAnalysisRules> </PropertyGroup> </Project>
If you do not want developers to be able to turn on extra rules over and above the minbar, do not add the ' $(CodeAnalysisRules); ' text.
Using Visual Studio, open your projects
In Solution Explorer, right-click a project and choose Unload Project
In Solution Explorer, right-click the unloaded project and choose Edit
Add the following <Import> element to the project, replacing [path] with the path of the targets file created above. This needs to be the last line (just above the closing </Project> element) to ensure that settings within the project do not override the team settings:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportProject=" [path] " /> </Project>
Repeat from Step 11 for each project you want to share the rule settings with
Any projects that imported the common rule settings via Step 9, should now reflect these settings in the Code Analysis properties pane. You can also share any MSBuild properties using similar steps.
Please Note: In order to avoid Visual Studio prompting about unsafe imported MSBuild projects, you need to explicitly trust the [team].CodeAnalysis.Rules.targets file. To do this, use the following:
- Run regedit
- Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\MSBuild\SafeImports
- Add a new string value called [team].CodeAnalysis.Rules.targets and set its value to the full path of the targets file.