HowTo: Disable specific Code Analysis rules for a Team Build
By rickvdbosch
- 3 minutes read - 443 wordsThanks 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 😉 ).
<DIV>
Start Visual Studio and create a new project
</DIV>
<DIV>
Open the properties for the project and navigate to the 'Code Analysis' tab
</DIV>
<DIV>
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)
</DIV>
<DIV>
Close the project/solution
</DIV>
<DIV>
Open the .csproj file in a text editor like Notepad or UltraEdit
</DIV>
<DIV>
Find the tag named CodeAnalysisRules and copy the value of that tag, which looks something like this: <BR /><CodeAnalysisRules>-Microsoft.Globalization#CA1301;-Microsoft.Naming#CA1709;-Microsoft.Usage#CA2208</CodeAnalysisRules><BR /><BR />You now have a list containing all the rules you do not want enforced.
</DIV>
Now for the real deal: how to disable specific code analysis rules for a team build.
<DIV>
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)
</DIV>
<DIV>
Find the PropertyGroup tag containing the RunCodeAnalysis tag
</DIV>
<DIV>
Set the RunCodeAnalysis tag to an <A class="" href="https://blogs.msdn.com/manishagarwal/archive/2005/07/14/438807.aspx">appropriate value</A> (I used Always to override individual project settings)
</DIV>
<DIV>
Add a tag named CodeAnalysisRules, and paste the rules you copied in the steps numbered 1 to 6 above
</DIV>
<DIV>
Find the Microsoft.TeamFoundation.Build.targets file at the Team Build Server. It is typically located in C:Program FilesMSBuildMicrosoftVisualStudiov8.0TeamBuild
</DIV>
<DIV>
Open the .targets file, find the CoreCompile target and copy the entire target
</DIV>
<DIV>
Paste the CoreCompile target in the TFSBuild.proj at the end of the project file, before the </Project> tag
</DIV>
<DIV>
Find the following task in the CoreCompile target, this is the second task from the end of the CoreCompile target<BR /><BR /><!– Build using MSBuild task –><BR /> <MSBuild<BR /> Condition=” '@(SolutionToBuild)'!=” “<BR /> Projects=”@(SolutionToBuild)”<BR /> Properties=”Configuration=%(ConfigurationToBuild.FlavorToBuild);Platform=%(ConfigurationToBuild.PlatformToBuild);SkipInvalidConfigurations=true;VCBuildOverride=$(MSBuildProjectDirectory)TFSBuild.vsprops;FxCopDir=$(FxCopDir);OutDir=$(OutDir);ReferencePath=$(ReferencePath);TeamBuildConstants=$(TeamBuildConstants);$(CodeAnalysisOption)”<BR /> Targets=”Build” /><BR />
</DIV>
<DIV>
Import your CodeAnalysisRules to the task in the Properties attribute (see the red part):<BR /><BR /><!– Build using MSBuild task –><BR /> <MSBuild<BR /> Condition=” '@(SolutionToBuild)'!=” “<BR /> Projects=”@(SolutionToBuild)”<BR /> Properties=”Configuration=%(ConfigurationToBuild.FlavorToBuild);Platform=% (ConfigurationToBuild.PlatformToBuild); SkipInvalidConfigurations=true;VCBuildOverride=$(MSBuildProjectDirectory)TFSBuild.vsprops;FxCopDir=$(FxCopDir);OutDir=$(OutDir);ReferencePath=$(ReferencePath); TeamBuildConstants=$(TeamBuildConstants);$(CodeAnalysisOption)<FONT color="red">;CodeAnalysisRules=$(CodeAnalysisRules)</FONT>“<BR /> Targets=”Build” />
</DIV>
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!