HowTo: Disable specific Code Analysis rules for a Team Build
Thanks to the MSDN forums (and specifically one Hua Chen), I found a way to disable specific code analysis rules for a team build. In this post I'll combine the steps I followed earlier with the description given at the MSDN forum. I'll start out with some steps earlier on in the process: how to quickly get a list of all the code analysis rules you want to disable (without having to type it all yourself ;) ).
-
Start Visual Studio and create a new project
-
Open the properties for the project and navigate to the 'Code Analysis' tab
-
Set the code analysis rules the way you want them, and save the project (you don't have to enable code analysis: the settings are saved anyway)
-
Close the project/solution
-
Open the .csproj file in a text editor like Notepad or UltraEdit
-
Find the tag named CodeAnalysisRules and copy the value of that tag, which looks something like this:
<CodeAnalysisRules>-Microsoft.Globalization#CA1301;-Microsoft.Naming#CA1709;-Microsoft.Usage#CA2208</CodeAnalysisRules>
You now have a list containing all the rules you do not want enforced.
Now for the real deal: how to disable specific code analysis rules for a team build.
-
Open the TFSBuild.proj file in a text editor (double clicking the file from the source control explorer will open it in Visual Studio, which works fine as an editor)
-
Find the PropertyGroup tag containing the RunCodeAnalysis tag
-
Set the RunCodeAnalysis tag to an
appropriate value (I used Always to override individual project settings)
-
Add a tag named CodeAnalysisRules, and paste the rules you copied in the steps numbered 1 to 6 above
-
Find the Microsoft.TeamFoundation.Build.targets file at the Team Build Server. It is typically located in C:\Program Files\MSBuild\Microsoft\VisualStudio\v8.0\TeamBuild
-
Open the .targets file, find the CoreCompile target and copy the entire target
-
Paste the CoreCompile target in the TFSBuild.proj at the end of the project file, before the </Project> tag
-
Find the following task in the CoreCompile target, this is the second task from the end of the CoreCompile target
<!-- Build using MSBuild task -->
<MSBuild
Condition=" '@(SolutionToBuild)'!='' "
Projects="@(SolutionToBuild)"
Properties="Configuration=%(ConfigurationToBuild.FlavorToBuild);Platform=%(ConfigurationToBuild.PlatformToBuild);SkipInvalidConfigurations=true;VCBuildOverride=$(MSBuildProjectDirectory)\TFSBuild.vsprops;FxCopDir=$(FxCopDir);OutDir=$(OutDir);ReferencePath=$(ReferencePath);TeamBuildConstants=$(TeamBuildConstants);$(CodeAnalysisOption)"
Targets="Build" />
-
Import your CodeAnalysisRules to the task in the Properties attribute (see the red part):
<!-- Build using MSBuild task -->
<MSBuild
Condition=" '@(SolutionToBuild)'!='' "
Projects="@(SolutionToBuild)"
Properties="Configuration=%(ConfigurationToBuild.FlavorToBuild);Platform=% (ConfigurationToBuild.PlatformToBuild); SkipInvalidConfigurations=true;VCBuildOverride=$(MSBuildProjectDirectory)\TFSBuild.vsprops;FxCopDir=$(FxCopDir);OutDir=$(OutDir);ReferencePath=$(ReferencePath); TeamBuildConstants=$(TeamBuildConstants);$(CodeAnalysisOption);CodeAnalysisRules=$(CodeAnalysisRules)"
Targets="Build" />
That's all of it. The rules you selected not to run (and pasted in the CodeAnalysisRules tag) will not be executed during the next build. Hope this helps!