How to make Sonarqube exclude a .NET (C#) project from coverage measures

一个人想着一个人 提交于 2019-12-03 13:39:45

Not enough rep to comment, but if you apply both answers here, you can simply add:

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

in the .csproj file to exclude the entire project instead of individual files, or relying on sonarqube filters.

I had the same problem, and it took me a while to figure it out:

Set up a Directory.Build.props file in the solution directory with this content:

<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>

This fragment will be included in all project files as explained here https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

Set the regex in the environment variable SQExclusionFilter accordingly. Use double-backslashes for path separation. In windows shell escape pipe characters with a caret: e.g.

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

Credits to Duncan Pocklington from SQ/MS!

After trying a thousand things I finally found the solution. It's necessary to add an item group in the .csproj.

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

Some notion :

A .NET solution is Sonarqube's project. A .NET solutions's project is Sonarqube's project's module.

I find two solutions to exlcure Sonarqube's module (.NET project) from code coverage.

1) Configure exclusion from Web UI Sonarqube

From : SonarQube MSBuild Scanner doesn't exclude files from analysis

  • Open Sonarqube's project -> Code (in top menu)
  • Open Sonarqube's module (left icon 'Open Component's Page) -> Administration (in top menu) -> General Settings
  • Add ** in Codecoverage exclusion :

(Screenshot in French, but you get the idea)

2) Add the property "sonar.coverage.exclusions" in csproj

In your <.NET project>.csproj, add :

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

This two solutions work for me, but I would prefer a global setting for Sonarqube's project (.NET Solution).

Summing up the above mentioned answers and also adding one point to it.

  1. To exclude a project from SonarQube Analysis from csproj we can achieve by adding the below code in .csproj of that project

    <PropertyGroup>
    <!-- Exclude the project from analysis -->
    <SonarQubeExclude>true</SonarQubeExclude>
    </PropertyGroup>
    
  2. To exclude a file from a project

     <ItemGroup>
     <SonarQubeSetting Include="sonar.coverage.exclusions">
     <Value>**/FileName.cs</Value>
     </SonarQubeSetting>
     </ItemGroup>
    
  3. And for multiple files

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

Also can refer this for regex patterns used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!