SonarQube MSBuild fails to exclude files

这一生的挚爱 提交于 2019-12-31 04:43:09

问题


I am running an analysis using msbuild on debian, using the following command:

 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>

However in the end command:

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

and the analysis on the UI of my server includes files from test/ folder.

Why does it fail to ignore the specific files?

Using SonarQube 6.7 and sonar-scanner:3.3


回答1:


Exclusions are difficult to set correctly from the analysis side, as demonstrated by your attempt. Your best bet is to set these from the UI.




回答2:


The only way I have managed to get around this situation is by adding the following line to the .csproj file of the projects I want excluded

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



回答3:


I had the same problem with after I upgraded to SonarQube Scanner for MSBuild 4.0.2

As pkaramol stated and by looking at the docs [1,2] this seems to be the only solution because sonar.exclusions matches only files in each project folder and not the solution folder. I wrote a python (>= 3.5) script for my CI which adds those lines to projects I want to be excluded.

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


来源:https://stackoverflow.com/questions/48685514/sonarqube-msbuild-fails-to-exclude-files

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