禁用代码分析输出 MSBuild

Posted

技术标签:

【中文标题】禁用代码分析输出 MSBuild【英文标题】:Disable Code Analysis output MSBuild 【发布时间】:2014-05-26 12:13:43 【问题描述】:

我正在使用构建定义构建项目。执行此操作时,还会执行代码分析。代码分析输出各种文件,包括:

ConsoleApplication2.exe.CodeAnalysisLog.xml
ConsoleApplication2.exe.lastcodeanalysissucceeded

有什么方法可以在我的构建定义中禁止这个输出发生,比如通过参数或类似的东西?

【问题讨论】:

【参考方案1】:

可以使用CodeAnalysisGenerateSuccessFile 选项禁用成功标记文件的生成。 例如

<CodeAnalysisGenerateSuccessFile>false</CodeAnalysisGenerateSuccessFile>

没有阻止生成日志文件的选项,但您可以通过CodeAnalysisLogFile 选项将其移动到其他位置。例如,要将其放在项目根文件夹中,您可以使用以下内容:

<CodeAnalysisLogFile>CodeAnalysisLog.xml</CodeAnalysisLogFile>

【讨论】:

你把这个XML标签&lt;CodeAnalysisGenerateSuccessFile&gt;放在哪里了? 如果我把它放在&lt;Project ...&gt;*.vcxproj下,那么它不起作用并产生错误尝试重新加载项目时。【参考方案2】:

我找到了lastcodeanalysissucceededfile 的选项。

通过提供输入参数:/p:CodeAnalysisGenerateSuccessFile=false,不会生成文件。

我还没有找到禁用日志文件输出的输入参数。如下所述的架构设置没有涵盖它。

<xs:element name="CodeAnalysisInputAssembly" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisInputAssembly" _locComment="" -->Path to the assembly to be analyzed by Code Analysis. The default is '$(OutDir)$(TargetName)$(TargetExt)'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisLogFile" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisLogFile" _locComment="" -->Path to the output file for the Code Analysis report. The default is '$(CodeAnalysisInputAssembly).CodeAnalysisLog.xml'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisLogFileXsl" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisLogFileXsl" _locComment="" -->Path to the XSL style sheet to reference in the Code Analysis output report. This report is specified in $(CodeAnalysisLogFile). The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisModuleSuppressionsFile" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisModuleSuppressionsFile" _locComment="" -->Name of the file, without the path, where Code Analysis project-level suppressions are stored. The default is 'GlobalSuppressions$(DefaultLanguageSourceExtension)'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisOverrideRuleVisibilities" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisOverrideRuleVisibilities" _locComment="" -->Indicates whether to run all overridable Code Analysis rules against all targets. This will cause specific rules, such as those within the Design and Naming categories, to run against both public and internal APIs, instead of only public APIs. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisOutputToConsole" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisOutputToConsole" _locComment="" -->Indicates whether to output Code Analysis warnings and errors to the console. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisVerbose" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisVerbose" _locComment="" -->Indicates whether to output verbose Code Analysis diagnostic info to the console. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisPath" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisPath" _locComment="" -->Path to the Code Analysis installation folder. The default is '$(VSINSTALLDIR)\Team Tools\Static Analysis Tools\FxCop'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisPlatformPath" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisPlatformPath" _locComment="" -->Path to the .NET Framework folder that contains platform assemblies, such as mscorlib.dll and System.dll. The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisProject" type="msb:StringPropertyType"  substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisProject" _locComment="" -->Path to the Code Analysis project (*.fxcop) to load. The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisQuiet" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisQuiet" _locComment="" -->Indicates whether to suppress all Code Analysis console output other than errors and warnings. This applies when $(CodeAnalysisOutputToConsole) is true. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleAssemblies" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleAssemblies" _locComment="" -->Semicolon-separated list of paths either to Code Analysis rule assemblies or to folders that contain Code Analysis rule assemblies. The paths are in the form '[+|-][!][file|folder]', where '+' enables all rules in rule assembly, '-' disables all rules in rule assembly, and '!' causes all rules in rule assembly to be treated as errors. For example '+D:\Projects\Rules\NamingRules.dll;+!D:\Projects\Rules\SecurityRules.dll'. The default is '$(CodeAnalysisPath)\Rules'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleDirectories" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleDirectories" _locComment="" -->Semicolon-separated list of directories in which to search for rules when resolving a rule set. The default is '$(CodeAnalysisPath)\Rules' unless the CodeAnalysisIgnoreBuiltInRules property is set to true.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRules" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRules" _locComment="" -->Semicolon-separated list of Code Analysis rules. The rules are in the form '[+|-][!]Category#CheckId', where '+' enables the rule, '-' disables the rule, and '!' causes the rule to be treated as an error. For example, '-Microsoft.Naming#CA1700;+!Microsoft.Naming#CA1701'. The default is an empty string ('') which enables all rules.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleSet" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleSet" _locComment="" -->A .ruleset file which contains a list of rules to run during analysis. The string can be a full path, a path relative to the project file, or a file name. If a file name is specified, the CodeAnalysisRuleSetDirectories property will be searched to find the file. The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleSetDirectories" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleSetDirectories" _locComment="" -->Semicolon-separated list of directories in which to search for rule sets. The default is '$(VSINSTALLDIR)\Team Tools\Static Analysis Tools\Rule Sets' unless the CodeAnalysisIgnoreBuiltInRuleSets property is set to true.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisSaveMessagesToReport" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisSaveMessagesToReport" _locComment="" -->Comma-separated list of the type ('Active', 'Excluded', or 'Absent') of warnings and errors to save to the output report file. The default is 'Active'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisSearchGlobalAssemblyCache" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisSearchGlobalAssemblyCache" _locComment="" -->Indicates whether Code Analysis should search the Global Assembly Cache (GAC) for missing references that are encountered during analysis. The default is true.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisSummary" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisSummary" _locComment="" -->Indicates whether to output a Code Analysis summary to the console after analysis. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisTimeout" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisTimeout" _locComment="" -->The time, in seconds, that Code Analysis should wait for analysis of a single item to complete before it aborts analysis. Specify 0 to cause Code Analysis to wait indefinitely. The default is 120.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisTreatWarningsAsErrors" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisTreatWarningsAsErrors" _locComment="" -->Indicates whether to treat all Code Analysis warnings as errors. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisUpdateProject" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisUpdateProject" _locComment="" -->Indicates whether to update the Code Analysis project (*.fxcop) specified in $(CodeAnalysisProject). This applies when there are changes during analysis. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisUseTypeNameInSuppression" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisUseTypeNameInSuppression" _locComment="" -->Indicates whether to include the name of the rule when Code Analysis emits a suppression. The default is true.</xs:documentation>
    </xs:annotation>

【讨论】:

你在哪里提供了标志p:CodeAnalysisGenerateSuccessFile=false?右键单击我的 Visual C++ 项目并转到 Properties -&gt; C/C++ -&gt; Command Line 并将此标志放入“其他选项”并没有停止它的生成。【参考方案3】:

我对 Matthijs 和 Nicole Calinoiu 提供的两个答案都不成功,但经过进一步调查,我在 *** 上偶然发现了this question。

简而言之,一般不建议禁用这些文件的输出。根据 seva titov 在上面链接的问题中的回答:

首先,删除 .lastcodeanalysissucceededd 将导致代码分析重新运行,即使没有任何更改。其次,删除 .CodeAnalysisLog.xml 将使得几乎不可能调查分析错误和警告的细节。所以你干脆把项目的代码分析关掉就好了。

因此,最好只是将这两个文件移动到中间文件位置。此位置是所有*.obj 文件、*.log 文件和其他元文件所在的位置。为此,请右键单击您的项目并单击“卸载项目。”

然后,右键单击该项目并单击Edit p͟r͟o͟j͟e͟c͟t͟.vcxproj。图中我点了Edit Test1.vcxproj,因为我这个演示的项目叫Test1

然后,在项目标签&lt;/Project&gt; 的末尾,您会看到一堆&lt;Import ...&gt; 语句。将the question 中描述的属性组放在这些导入语句之后:

<PropertyGroup>
    <CodeAnalysisLogFile>$(IntermediateOutputPath)$(TargetFileName).CodeAnalysisLog.xml</CodeAnalysisLogFile>
    <CodeAnalysisSucceededFile>$(IntermediateOutputPath)$(TargetFileName).lastcodeanalysissucceeded</CodeAnalysisSucceededFile>
</PropertyGroup>

例如,它应该如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <!-- 
        ...
        A lot of other stuff not shown
        ...
    -->
    
    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    <ImportGroup Label="ExtensionTargets">
    </ImportGroup>

    <!-- The following will move the code analysis output files and put them in the intermediate folder -->
    <PropertyGroup>
        <CodeAnalysisLogFile>$(IntermediateOutputPath)$(TargetFileName).CodeAnalysisLog.xml</CodeAnalysisLogFile>
        <CodeAnalysisSucceededFile>$(IntermediateOutputPath)$(TargetFileName).lastcodeanalysissucceeded</CodeAnalysisSucceededFile>
    </PropertyGroup>
</Project>

(注意: 如上所示,将&lt;CodeAnalysisSucceededFile&gt;&lt;CodeAnalysisLogFile&gt; 标记的内容保留在一行中。Visual Studio 2019 抛出“路径中的非法字符”错误,如果您将内容放在多行中。)

【讨论】:

以上是关于禁用代码分析输出 MSBuild的主要内容,如果未能解决你的问题,请参考以下文章

通过 Devenv 命令行构建时禁用代码分析

MSBuild 找不到代码分析工具

知道为啥 MSBuild 会突然开始对我的解决方案中的其他项目执行代码分析吗?

是否可以在未安装 VS 的情况下从 MSBuild 命令行运行 FxCop 代码分析?

只有 msbuild 14 或 msbuild 15 支持 SonarQube 分析

如何在 Visual Studio 2008 中禁用 XAML 文件的“代码分析”?