【问题标题】:How to make Sonarqube exclude a .NET (C#) project from coverage measures如何使 Sonarqube 从覆盖度量中排除 .NET (C#) 项目
【发布时间】:2018-06-04 23:12:10
【问题描述】:

Sonarqube 允许通过在 sonar.coverage.exclusions 键中添加模式来将单个文件排除在代码覆盖范围之外。这可以通过将它们添加到 UI 甚至 .csproj 文件中(通过指定 SonarQubeSetting 元素)在项目级别上完成。即

<SonarQubeSetting Include="sonar.coverage.exclusions"> <Value>**/*.cs</Value> </SonarQubeSetting>

但是,这两种方法似乎都不起作用。使用 SonarQube documentation 中指定的模式不会提供所需的结果。我也知道 SonarQubeExclude MSBuild 属性的存在,但我不想走这条路,因为它会将我的项目排除在所有其他分析之外。

还有另一种我失踪的可能性吗?还是根本不可能将项目中的所有类都排除在代码覆盖范围之外?

【问题讨论】:

  • 您是否尝试在命令行中像这样传递参数:SonarQube.Scanner.MSBuild.exe" begin /d:sonar.coverage.exclusions=**\MyLib1\MyPath1*,**\MyLib2\MyPath2* 在我的情况下就像一个魅力。
  • @Peska 您正在运行什么版本的 SonarQube/MSBuild 扫描仪?在我的情况下,这没有按预期工作。
  • SonarQube 版本 6.7.1.35068。 SonarScanner MSBuild 4.0.2.892。这是一个完整的示例:"C:\PathToSonarQubeScanner\SonarQube.Scanner.MSBuild.exe" begin /k:"ProjectKey" /n:"ProjectName" /v:"1.0.0" /d:sonar.coverage.exclusions=**\PathToExclude\**,**\PathToExlude2\**"C:\PathToMSBuild\MSBuild.exe" SolutionFile.sln /t:Rebuild"C:\PathToSonarQubeScanner\SonarQube.Scanner.MSBuild.exe" end

标签: c# msbuild sonarqube csproj sonarqube-msbuild-runner


【解决方案1】:

总结上面提到的答案,并加一分。

  1. 要从 csproj 的 SonarQube 分析中排除项目,我们可以通过在该项目的 .csproj 中添加以下代码来实现

    <PropertyGroup>
    <!-- Exclude the project from analysis -->
    <SonarQubeExclude>true</SonarQubeExclude>
    </PropertyGroup>
    
  2. 从项目中排除文件

     <ItemGroup>
     <SonarQubeSetting Include="sonar.coverage.exclusions">
     <Value>**/FileName.cs</Value>
     </SonarQubeSetting>
     </ItemGroup>
    
  3. 对于多个文件

    <ItemGroup>
    <SonarQubeSetting Include="sonar.coverage.exclusions">
    <Value>**/FileName1.cs, **/FileName2.cs</Value>
    </SonarQubeSetting>
    </ItemGroup>
    

也可以参考这个regex patterns使用

【讨论】:

    【解决方案2】:

    没有足够的代表发表评论,但如果您在这里同时应用两个答案,您可以简单地添加:

    <PropertyGroup>
        <!-- Exclude the project from analysis -->
        <SonarQubeExclude>true</SonarQubeExclude>
    </PropertyGroup>
    

    在 .csproj 文件中排除整个项目而不是单个文件,或者依靠声纳过滤器。

    【讨论】:

      【解决方案3】:

      一些概念:

      .NET 解决方案是 SonarQube 的项目。 .NET 解决方案的项目是 SonarQube 项目的模块。

      特定的 SonarQube MsBuild 分析器将找到所有 csproj/vbproj。 对于每个 .NET 项目,分析器都会应用 SonarQube 的项目设置。

      如果您的存储库是:

      MyApp.sln
      MyApp.Shell\
          MyApp.Shell.csproj
          Windows\MainWindows.cs
      MyApp.Core\
          MyApp.Core.csproj
          FooService.cs
      

      要忽略 MyApp.Shell 项目的代码覆盖率,您将直观地添加设置 sonar.coverage.exclusions=MyApp.Shell\*。 但是 MsBuild 分析器分别分析每个 .NET 项目,并且根目录直接位于 csproj/vbproj 目录。 分析 MyApp.Shell.csproj 时,忽略模式 MyApp.Shell\* 应用于 Windows\MainWindows.cs

      要从代码覆盖范围中排除特定项目,我们需要将此设置应用于 SonarQube 的模块(.NET 项目)级别。 我找到了两种解决方案。

      1)在csproj中添加属性“sonar.coverage.exclusions”

      在您的 <.net>.csproj 中,添加:

      <Project ...>
        ...
          <ItemGroup>
            <SonarQubeSetting Include="sonar.coverage.exclusions">
              <Value>**</Value>
            </SonarQubeSetting>
          </ItemGroup>
        ...
      </Project>
      

      2) 配置从 Web UI Sonarqube 中排除

      从更新到 SonarQube 8.*,我无法在 IHM 中检索该选项。

      发件人:SonarQube MSBuild Scanner doesn't exclude files from analysis

      • 打开 Sonarqube 的项目 -> 代码(在顶部菜单中)
      • 打开 Sonarqube 的模块(左侧图标“打开组件的页面”)-> 管理(在顶部菜单中)-> 常规设置
      • 在代码覆盖排除中添加**:

      (法语截图,但你懂的)

      这两个解决方案对我有用,但我更喜欢 Sonarqube 的项目(.NET 解决方案)的全局设置。

      【讨论】:

      • 对我来说,它只在删除''标签时才有效......
      • @FrankyHollywood,我使用的是旧的 SonarQube 版本 6.*,在更新到 8.* 之后,我还需要删除目标部分。我编辑了我的答案。
      【解决方案4】:

      在尝试了一千件事后,我终于找到了解决方案。 需要在.csproj中添加item group。

      <ItemGroup>
          <Compile Update="ClassToExclude.cs">
            <!-- Exclude the file from analysis -->
            <SonarQubeExclude>true</SonarQubeExclude>
          </Compile>
        </ItemGroup>
      

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,我花了一段时间才弄清楚:

        在解决方案目录中设置一个 Directory.Build.props 文件,内容如下:

        <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
        
          <!-- This target customises the SonarQube MSBuild runner targets to limit the projects that are analysed.
        
               Projects whose full path and file name match the specified filter will be marked as "excluded".
        
               Note that this targets file does not ever set $(SonarQubeExclude) to "false". This is to allow other
               targets to exclude projects for other reasons (e.g. a Fakes projects should always be excluded, regardless
               of whether or not they match the project filter).
        
               Also, this project will do nothing if $(SonarQubeExclude) has already been set.
        
               The regular expression uses the normal .NET regular expression syntax.
        
               Usage:
               (1) include the target in the projects being built, either by directly importing it or by
                   dropping it in the one of the standard "ImportBefore" folders.
        
               (2) set $(SQExclusionFilter) to the desired regular expression
                   e.g. the following example matches all projects with "CodeSense\Services" in the path:  .*\\CodeSense\\Services\\.*
        
          -->
          <PropertyGroup Condition=" $(SonarQubeExclude) == '' AND $(SQExclusionFilter) != '' ">
        
              <MatchesSQExclusionFilter Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectFullPath), $(SQExclusionFilter), System.Text.RegularExpressions.RegexOptions.IgnoreCase)) ">true</MatchesSQExclusionFilter>
              <SonarQubeExclude Condition="$(MatchesSQExclusionFilter) == 'true' " >true</SonarQubeExclude>
        
          </PropertyGroup>
        
          <!-- This target is not required: it simply writes out additional information to simplify debugging -->
          <Target Name="AnalysisProjectInfoExclude_DEBUG" BeforeTargets="CoreCompile"
                  Condition="$(SQExclusionFilter) != '' ">
        
            <Message Importance="high" Text="ExclusionFilter: filter has been set.  Filter= $(SQExclusionFilter)" />
            <Message Importance="high" Text="ExclusionFilter: current project = $(MSBuildProjectFullPath)" />
            <Message Importance="high" Text="ExclusionFilter: match result = $(MatchesSQExclusionFilter)" />
        
            <Message Importance="high" Condition="$(MatchesSQExclusionFilter) == 'true' "
               Text="ExclusionFilter: project is excluded" />
        
            <Message Importance="high" Condition="$(MatchesSQExclusionFilter) != 'true' "
               Text="ExclusionFilter: project has not been excluded by the exclusion filter" />
        
          </Target>
        
        </Project>
        

        此片段将包含在所有项目文件中,如此处所述https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

        在环境变量SQExclusionFilter中相应地设置正则表达式。使用双反斜杠进行路径分隔。在带有插入符号的 Windows shell 转义管道字符中:例如

        set SQExclusionFilter=(path1\\project)^|(path2\\project)
        

        感谢来自 SQ/MS 的 Duncan Pocklington!

        【讨论】:

        • 我查看了您的解决方案,但没有看到这些 MSBuild 变量与 SonarQube 之间的关系。这些变量是否在 SonarQube 中预定义?是否也可能是您试图排除整个项目而我只想排除代码覆盖率?
        • @mvandevy,你是对的,解决方案将一个项目排除在所有分析之外。如果过滤器命中一个项目,它会添加一个 XML 片段 true 到项目中,从而抑制该项目的 SonarQube。
        • 在 SonarQube 的代码覆盖分析中仅排除一个文件就需要这种复杂性,这太可怕了。
        【解决方案6】:

        这是一个老问题,但更准确的答案贴在这里:https://stackoverflow.com/a/49904805/987191

        基本上,代码选项卡允许深入到项目的设置,我们可以在管理(项目)中使用 ** -> 常规设置 -> 分析范围 -> 覆盖范围排除

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-25
          • 2018-11-17
          • 2021-12-29
          • 2017-01-24
          • 2019-05-06
          • 2017-01-10
          • 1970-01-01
          • 2016-04-03
          相关资源
          最近更新 更多