使用MSBuild 14时禁用代码分析 [英] Disable code analysis when using MSBuild 14

查看:118
本文介绍了使用MSBuild 14时禁用代码分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个C#6.0项目的.NET解决方案.每个项目都通过NuGet引用 StyleCop分析器.在Visual Studio中,我有可能在构建和分析代码之间进行区分,但是我看不到如何在命令行上(例如,在CI服务器上)使用MSBuild v14.0来做到这一点.我正在使用以下选项调用msbuild mySolution.sln /t:Rebuild,但没有一个起作用:

I have a .NET solution containing several C# 6.0 projects. Every project references the StyleCop Analyzer via NuGet. Within Visual Studio, I have the possibility to distinguish between building and analyzing the code, but I don't see how to do this with MSBuild v14.0 on the command line (e. g. on a CI server). I'm calling msbuild mySolution.sln /t:Rebuild with the following options, none of them worked:

  • /p:RunCodeAnalysis=False
  • /p:RunCodeAnalysisOnThisProject=False
  • /p:RunCodeAnalysis=False,RunCodeAnalysisOnThisProject=False
  • /p:RunCodeAnalysis=False
  • /p:RunCodeAnalysisOnThisProject=False
  • /p:RunCodeAnalysis=False,RunCodeAnalysisOnThisProject=False

无论我做什么,警告SAxxxx都会保留在输出中.有谁知道如何在使用MSBuild时禁用代码分析?

Whatever I do, the warnings SAxxxx remain in the output. Does anyone know how to disable code analysis when using MSBuild?

背景:在我们的CI服务器上,我想区分基本MSBuild警告"和来自静态代码分析的警告.

Background: on our CI server, I want to distinguish between "basic MSBuild warnings" and warnings coming from static code analysis.

致谢

推荐答案

有人知道如何在使用MSBuild时禁用代码分析吗?

anyone know how to disable code analysis when using MSBuild?

在构建服务器TFSBuild.proj中定义的RunCodeAnalysis设置与本地MSBuild项目架构选项有显着差异.

The RunCodeAnalysis setting as defined in the build server TFSBuild.proj differs significantly from the local MSBuild project schema options.

对于RunCodeAnalysis,构建服务器支持值为"始终,默认,从不".相比之下,本地MSBuild在RunCodeAnalysis中支持" True或False ".

Build server support the value of "Always, Default, Never" for RunCodeAnalysis. In contrast, locally MSBuild supports "True or False" for RunCodeAnalysis.

您可以检查Microsoft.TeamFoundation.Build.targets文件的部分:

You can check the section of the Microsoft.TeamFoundation.Build.targets file:

<Target Name="CoreCompileSolution">
 
  <PropertyGroup>
    <CodeAnalysisOption Condition=" '$(RunCodeAnalysis)'=='Always'">RunCodeAnalysis=true</CodeAnalysisOption>
    <CodeAnalysisOption Condition=" '$(RunCodeAnalysis)'=='Never'">RunCodeAnalysis=false</CodeAnalysisOption>
    <!-- ... -->
  </PropertyGroup>
  <!-- ... -->
</Target>

由此我们可以推断出,默认"设置没有为运行时提供值,而始终"和从不"分别映射为True/False.

From this we can infer that "Default" setting does not provide a value to the runtime, while "Always" and "Never" map to True/False respectively.

在构建服务器上:

Always告诉MSBuild使用RunCodeAnalysis = True编译所有项目

Always tells MSBuild to compile all projects with RunCodeAnalysis=True

Never告诉MSBuild在所有项目上禁止执行代码分析(RunCodeAnalysis = False).

Never tells MSBuild to suppress code analysis (RunCodeAnalysis=False) on all projects.

因此,RunCodeAnalysis的值为Default,Always,NeverTrue,False,具体取决于您的构建方式.

So the values for RunCodeAnalysis are either Default,Always,Never or True,False, depending on how you build.

您可以参考如何:编辑生成类型 CodeAnalysis,FxCop和团队建设以获取更多详细信息.

You can refer to the How to: Edit a Build Type and CodeAnalysis, FxCop and Team Build to more detailed info.

更新: 根据mu88回答,我已经使用RunCodeAnalysis=False在Jenkins上创建了一个测试演示,代码分析已按预期禁用.以下是我在Jenkins上的配置:

Update: According to the mu88 replied, I have create a test demo on the Jenkins with RunCodeAnalysis=False, the code analysis is disabled as expected. Below is my configuration on the Jenkins:

此外,您还可以检查构建日志中是否有从"Running Code Analysis..."到"Code Analysis Complete"的部分. 并且对于警告SAxxxx仍保留在输出中,这不是代码分析结果.您可以在Visual Studio上对其进行测试,而无需进行代码分析.安装软件包StyleCop.Analyzers,然后构建项目后,您将收到这些警告.

Besides, You can also check the build log whether has the section from "Running Code Analysis..." to "Code Analysis Complete " And for the warnings SAxxxx remain in the output, this is not Code Analysis result. You can test it on the Visual Studio without code analysis. After install the package StyleCop.Analyzers, then build the project, you will get those warnings.

因此,在使用参数:/p:RunCodeAnalysis=False构建项目之后,请仔细检查Jenkins的构建日志中是否包含"Running Code Analysis..."和"Code Analysis Complete"部分.

So please double check whether the build log on the Jenkins contains the section "Running Code Analysis..." and "Code Analysis Complete " after build the project with parameter:/p:RunCodeAnalysis=False.

Update2:

如果要禁止StyleCop警告,可以通过在.cs文件顶部添加以下标头来欺骗StyleCop根本不处理文件:

If you want to suppress StyleCop Warning, you can trick StyleCop into not processing a file at all by adding this header at the top of .cs file:

//------------------------------------------------------------------------------
// <auto-generated>
// Well, not really. This is just a trick to get StyleCop off my back.
// </auto-generated>
//------------------------------------------------------------------------------

这篇关于使用MSBuild 14时禁用代码分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆