繁体   English   中英

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下行添加到我要排除的项目的.csproj文件中

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

升级到 MSBuild 4.0.2 的 SonarQube 扫描仪后,我遇到了同样的问题

正如 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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM