Setting code analysis parameters in TFSBuild.proj

I am trying to set / override some parameters in our TEST TFS installation regarding forced code analysis and complicated settings during the build process (regardless of the installation of the project sin file)

We are currently using our TEST TFS installation:

  • Visual Studio 2012 Ultimate On Our Developer Machines And Build A Server
  • Install TFS 2012 on one server (application and data tier)
  • Install the TFS 2012 service (controller and agent) installed on another server

We can compile examples of .net 4.5 projects (class libraries (DLLs), web applications, etc.), as expected. This is only due to overriding the associated code analysis parameters (hopefully).

Scenario 1. In our examples of applications on our development machines, when choosing project parameters (right-click → properties in the solution explorer), go to the "Code analysis" tab if I turn on "Enable code analysis during assembly" and select a rule set from a dropdown that will be executed as exepcted, so it will generate some warnings. This technical file adds <RunCodeAnalysis>false</RunCodeAnalysis> to the * .csproj file if it is opened in notepad. If the assembly is performed to compile a sample project / solution, then code analysis is performed as expected. I DO NOT want to do this in every project, because the developer can disable it (although I want to have registration policies and / or private / private checks, so it’s all the same).

Scenario 2 - I can turn off the “Enable code analysis on assembly” checkbox and analyze the strength code in our TFSBuild.proj file (we (will) use defaultetemplate.xaml as our default process definition because we will update it with TFS 2008 on our LIVE TFS installation):

<RunCodeAnalysis>Always</RunCodeAnalysis>

This works, and that is how we will strengthen (the lessons that have yet to be studied :-)) Code analysis on our assemblies.

Then a problem arises when setting up other code analysis settings. For example, which default rule set to apply / use or handle CA warnings as errors. Some of these settings can be set either in VS or in all of them by editing * .csproj in notepad. If I edit * .csproj, then these values ​​are used in the assembly as expected (as well as locally on the developer's machine). This is not ideal as I want to do it centrally in TFSBuild.proj without having to edit every project file. I believe that I can use such settings as in the TFSbuild.proj file:

 <PropertyGroup> <RunCodeAnalysis>Always</RunCodeAnalysis> <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors> </PropertyGroup> 

But they don't seem to work, or am I putting them in the wrong place? How to fix / use them correctly?

FYI I create my solutions in TFSBuild.proj:

 <Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" /> <ItemGroup> <SolutionToBuild Include="/some folder/some solution.sln" /> <ConfigurationToBuild Include="Debug|Any CPU"> <FlavorToBuild>Debug</FlavorToBuild> <PlatformToBuild>Any CPU</PlatformToBuild> </ConfigurationToBuild> </ItemGroup> </Project> 

On the build server, I found a link to the target file for code analysis in the c: \ Program Files (x86) \ MSBuild \ Microsoft \ VisualStudio \ v11.0 \ CodeAnalysis directory, but I don’t want to change the default behavior to (although it works when I do it). The condition, for example, for CodeAnalysisTreatWarningsAsErrors, should be evaluated as false. Its like my values ​​are not read from TFSBuild.proj, but from the .csproj file.

Any questions feel free to ask and give thanks in advance

+7
source share
2 answers

I remember that I had a similar problem - but I did not have time to research it, I worked on it by calling FxCop directly using the exec task. I will just give you the main points, omitting the specifications of some properties, I hope the names are clear.

I created an ItemGroup of the output dlls FilesToAnalyze and passed it to FxCop in a way similar to:

 <PropertyGroup> <FxCopErrorLinePattern>: error</FxCopErrorLinePattern> <FxCopCommand>"$(FxCopPath)" /gac /rule:"$(FxCopRules)" /ruleset:="$(FxCopRuleSet)" @(FilesToAnalyze->'/file:"%(identity)"', ' ') /out:$(FullFxCopLog) /console | Find "$(FxCopErrorLinePattern)" > "$(FxCopLogFile)"</FxCopCommand> </PropertyGroup> <Exec Command="$(FxCopCommand)" ContinueOnError="true"> <Output TaskParameter="ExitCode" PropertyName="FxCopExitCode"/> </Exec> <ReadLinesFromFile File="$(FxCopLogFile)"> <Output TaskParameter="Lines" ItemName="AllErrorLines"/> </ReadLinesFromFile> 

Then I could determine the number of errors in the output using the extensionpack task:

 <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="GetItemCount" InputItems1="@(AllErrorLines)"> <Output TaskParameter="ItemCount" PropertyName="FxErrorCount"/> </MSBuild.ExtensionPack.Framework.MsBuildHelper> 

and create an unsuccessful build step for each error:

 <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Id="$(FxCopStep)" Status="Failed" Message="FxCop Failed: $(FxErrorCount) errors."/> <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Status="Failed" Message="%(AllErrorLines.Identity)"/> 

By performing code analysis on the build server this way, we also avoided setting up each project individually. We isolated all of this in a separate .targets file, so adding code analysis to the solution was a matter of importing this file and possibly adjusting the behavior by setting the appropriate properties.

+1
source

I had a similar problem with the fact that Cruise Control does not compile using the CODE_ANALYSIS compilation CODE_ANALYSIS , even if the option "Enable code analysis on assembly (defines the constant CODE_ANALYSIS)" was checked in VS.Net.

It looks like this is a check or not, CODE_ANALYSIS is actually not explicitly added to the list of compilation symbols in csproj (even if it appears in the Conditional Compilation Symbols text box), only <RunCodeAnalysis>true</RunCodeAnalysis> .

When compiling through VS.Net, CODE_ANALYSIS automatically added, but not when using MSBuild, which uses Cruise Control.

In the end, I changed VS.Net "conditional compilation symbols" from "CODE_ANALYSIS; MySymbol" to "MySymbol; CODE_ANALYSIS". Executing this forced CODE_ANALYSIS will also appear in csproj.

0
source

All Articles