SonarQube MSBuild 无法排除文件

SonarQube MSBuild fails to exclude files

我是运行在debian上使用msbuild进行分析,使用以下命令:

 mono /msbuild/SonarQube.Scanner.MSBuild.exe begin /d:sonar.login=<sonarqubetoken> /d:sonar.host.url=https://<my-server> /d:sonar.exclusions=test/**/* /k:<my-project-key>

但是end命令中:

INFO: Index files
INFO: Excluded sources: 
INFO:   test/**/*
INFO: 17 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: Quality profile for cs: Sonar way
INFO: Excluded sources for coverage: 
INFO:   test/**

我服务器 UI 的分析包括 test/ 文件夹中的文件。

为什么忽略特定文件失败?

使用 SonarQube 6.7sonar-scanner:3.3

排除项很难从分析方面正确设置,正如您的尝试所证明的那样。您最好的选择是从 UI.

中设置这些

我设法解决这种情况的唯一方法是将以下行添加到我要排除的项目的 .csproj 文件中

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

我升级到 SonarQube Scanner for MSBuild 4.0.2 后遇到了同样的问题

正如 pkaramol 所述,通过查看文档 [1,2] 这似乎是唯一的解决方案,因为 sonar.exclusions 仅匹配每个项目文件夹中的文件,而不匹配解决方案文件夹中的文件。我为我的 CI 编写了一个 python (>= 3.5) 脚本,它将这些行添加到我想排除的项目中。

import os
import glob
import shutil

SOURCEDIR_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

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

def add_sq_exclude_tag(project_name):
    search_path = os.path.join(SOURCEDIR_PATH, '**', '{}.csproj'.format(project_name))
    for file_path in glob.iglob(search_path, recursive=True):
        with open(file_path, 'r', encoding='utf8') as outfile:
            lines = outfile.readlines()
            project_end_tag = lines[-1]
            lines[-1] = SQ_EXCLUDE_TAG
            lines.append(project_end_tag)
        with open(file_path, 'w', encoding='utf8') as outfile:
            outfile.writelines(lines)
        print('Added sonarqube exclude tag to {}'.format(file_path))


if __name__ == '__main__':
    add_sq_exclude_tag('*csprojFileConatainsThisString*')
    add_sq_exclude_tag('exactCsprojFileNameWithoutFileEnding')