如何使Sonarqube从覆盖率度量中排除.NET(C#)项目

mva*_*evy 7 c# msbuild csproj sonarqube sonarqube-msbuild-runner

Sonarqube允许通过在sonar.coverage.exclusions键中添加模式来从代码覆盖中排除单个文件.这可以在项目级别上完成,方法是在UI中添加它们,甚至在.csproj文件中通过指定SonarQubeSetting元素来完成.即

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

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

我还有另一种可能性吗?或者是否根本无法从代码覆盖中排除项目中的所有类?

Nav*_*mar 21

总结上面提到的答案,并补充一点。

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

    <PropertyGroup>
    <!-- Exclude the project from analysis -->
    <SonarQubeExclude>true</SonarQubeExclude>
    </PropertyGroup>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 从项目中排除文件

     <ItemGroup>
     <SonarQubeSetting Include="sonar.coverage.exclusions">
     <Value>**/FileName.cs</Value>
     </SonarQubeSetting>
     </ItemGroup>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 对于多个文件

    <ItemGroup>
    <SonarQubeSetting Include="sonar.coverage.exclusions">
    <Value>**/FileName1.cs, **/FileName2.cs</Value>
    </SonarQubeSetting>
    </ItemGroup>
    
    Run Code Online (Sandbox Code Playgroud)

也可以参考这个对正则表达式模式中使用


Jon*_*med 7

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

<PropertyGroup>
    <!-- Exclude the project from analysis -->
    <SonarQubeExclude>true</SonarQubeExclude>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

在 .csproj 文件中排除整个项目而不是单个文件,或依赖 sonarqube 过滤器。


Orw*_*wel 6

一些想法:

.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
Run Code Online (Sandbox Code Playgroud)

要忽略 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 中,添加:

MyApp.sln
MyApp.Shell\
    MyApp.Shell.csproj
    Windows\MainWindows.cs
MyApp.Core\
    MyApp.Core.csproj
    FooService.cs
Run Code Online (Sandbox Code Playgroud)

2) 配置 Web UI Sonarqube 排除

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

来自:SonarQube MSBuild Scanner 不会从分析中排除文件

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

(屏幕截图是法语,但你明白了) 声纳库 Web UI

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


And*_*nca 5

在尝试了一千种方法之后,我终于找到了解决方案。需要在 .csproj 中添加项目组。

<ItemGroup>
    <Compile Update="ClassToExclude.cs">
      <!-- Exclude the file from analysis -->
      <SonarQubeExclude>true</SonarQubeExclude>
    </Compile>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)


Rei*_*ayr 1

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

在解决方案目录中设置一个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>
Run Code Online (Sandbox Code Playgroud)

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

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

set SQExclusionFilter=(path1\\project)^|(path2\\project)
Run Code Online (Sandbox Code Playgroud)

感谢来自 SQ/MS 的 Duncan Pocklington!

  • 可怕的是,仅仅为了从 SonarQube 中的代码覆盖率分析中排除文件就需要这种复杂性。 (4认同)