C# SonarQube MSBuild无法排除文件

C# SonarQube MSBuild无法排除文件,c#,sonarqube,sonarqube-scan,C#,Sonarqube,Sonarqube Scan,我使用以下命令在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> 我的服务器UI上的分析包括test/文件夹中的文件 为什么它

我使用以下命令在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>
我的服务器UI上的分析包括
test/
文件夹中的文件

为什么它不能忽略特定的文件


如您的尝试所示,使用声纳QUBE 6.7和声纳扫描仪:3.3很难从分析方面正确设置排除项。您最好从UI设置这些选项。

我唯一能够避免这种情况的方法是将以下行添加到我想要排除的项目的
.csproj
文件中

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

真的

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

正如pkaramol所述,通过查看文档[,],这似乎是唯一的解决方案,因为sonar.Exclutions只匹配每个项目文件夹中的文件,而不匹配解决方案文件夹。我为我的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')
导入操作系统
导入glob
进口舒蒂尔
SOURCEDIR\u PATH=os.PATH.dirname(os.PATH.dirname(os.PATH.dirname(os.PATH.abspath(_文件__)))
SQ_EXCLUDE_TAG=”“”
真的
"""
def add_sq_exclude_标签(项目名称):
search_path=os.path.join(SOURCEDIR_path,'**','{}.csproj'.格式(项目名称))
对于glob.iglob中的文件路径(search\u path,recursive=True):
打开(文件路径为'r',编码为'utf8')作为输出文件:
lines=outfile.readlines()
项目结束标记=行[-1]
行[-1]=SQ_排除标记
行。追加(项目\结束\标记)
打开(文件路径为'w',编码为'utf8')作为输出文件:
输出文件写入线(行)
打印('已将排除标记添加到{}'。格式(文件路径))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
添加_sq_exclude_标记('*csprojfileconatainsthistring*'))
添加\u sq\u exclude\u标记('ExactCSProjFileName WithOutfileEnding')

LGTM。它在根目录中肯定是
test/
,而不是
tests/
[space]test/
?而且您使用的是
mono
所以区分大小写的文件系统?路径和大小写都是正确的。。。不知道发生了什么这对我不起作用。我们仍无法处理该项目。这不是非常自动化或可扩展。我不认为这是一个解决大型软件占用的公司的解决方案。
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')