SonarQube MSBuild fails to exclude files

青春壹個敷衍的年華 提交于 2019-12-02 04:12:49

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.

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>

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