SonarQube MSBuild Scanner不会从分析中排除文件 [英] SonarQube MSBuild Scanner doesn't exclude files from analysis

查看:613
本文介绍了SonarQube MSBuild Scanner不会从分析中排除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在将SonarQube Scanner用于VSTS/TFS 4.1.1(正在使用SonarQube Scanner 4.1.1).

We are currently using SonarQube Scanner for VSTS/TFS 4.1.1 (which is using SonarQube Scanner 4.1.1).

对于我们解决方案中的某些项目(模块),我们希望从分析和代码覆盖率统计信息中排除文件.应该通过使用文件模式而不修改TFS Build任务来以可维护的方式完成此操作.

For some projects (modules) in our solution we want to exclude files from the analysis and from the code coverage statistics. This should be done in a maintainable way by using file patterns and not modifying the TFS Build task.

    |- Source
      |- ProjectA
        |- Scripts (should be excluded)
        |- OwnCode
      |- ProjectB
        |- Views (only code coverage should be excluded)
        |- Presenters
        |- ...
      |- ProjectC
        |- Scripts (should be scanned)
        |- ...
      |- ...
      |- Solution.sln

我们尝试过的事情:

  1. 不包括在TFS构建任务中
    • 绝对路径(带有反斜杠或斜杠):sonar.exclusions="$(Build.SourcesDirectory)\Source\ProjectA\Scripts\**\*.js"(覆盖范围类似)
    • 相对路径:**/ProjectsA/Scripts/**/*.js
  1. Excluding in the TFS Build Task
    • Absoulte path (with backslash or slash):sonar.exclusions="$(Build.SourcesDirectory)\Source\ProjectA\Scripts\**\*.js" (similar for the coverage)
    • Relative path: **/ProjectsA/Scripts/**/*.js
  • 分析排除:**/ProjectA/Scripts/**/*.js
  • 覆盖率范围:**/ProjectB/Views/**/*.cs
  • Analysis exclusion: **/ProjectA/Scripts/**/*.js
  • Coverage exclusion: **/ProjectB/Views/**/*.cs
  • 不受支持,并导致以下错误:sonar-project.properties files are not understood by the SonarScanner for MSBuild

我们所看到的:

SonarQube Web界面中扫描器上下文的日志为:

What we see:

The logs of the Scanner Context in the SonarQube web interface are:

  Settings for module: Solution:Solution:6FA7B5C2-667D-4387-98B9-445617F2AC0B
  - sonar.coverage.exclusions=**/ProjectA/Views/**/*.cs
  - sonar.cs.analyzer.projectOutPath=D:\agent1\_work\5\.sonarqube\out\9
  - sonar.cs.analyzer.projectOutPaths="D:\agent1\_work\5\.sonarqube\out\9"
  - sonar.cs.roslyn.reportFilePath=D:\agent1\_work\5\s\Source\Solution\Source\ProjectA\bin\Release\ProjectA.dll.RoslynCA.json
  - sonar.cs.roslyn.reportFilePaths="D:\agent1\_work\5\s\Source\Solution\Source\ProjectA\bin\Release\ProjectA.dll.RoslynCA.json"
  - sonar.exclusions=**/ProjectA/Scripts/**/*.js
  - sonar.moduleKey=Solution:Solution:6FA7B5C2-667D-4387-98B9-445617F2AC0B
  - sonar.projectBaseDir=D:\agent1\_work\5\s\Source\Solution\Source\ProjectA
  - sonar.projectKey=Solution:Solution:6FA7B5C2-667D-4387-98B9-445617F2AC0B
  - sonar.projectName=ProjectA
  - sonar.sourceEncoding=utf-8
  - sonar.sources="D:\agent1\_work\5\s\Source\Solution\Source\ProjectA\Scripts\abc.js","..."

TFS版本中MSBuild扫描仪的日志为:

The logs of the MSBuild Scanner in the TFS build are:

Base dir: D:\agent1\_work\5\s\Source\Solution\Source\ProjectA
Working dir: D:\agent1\_work\5\.sonarqube\out\.sonar\Solution_Solution_6FA7B5C2-667D-4387-98B9-445617F2AC0B
Source paths: Scripts/abc.cs, ...
Source encoding: UTF-8, default locale: en_US
Index files
Excluded sources: 
  **/ProjectA/Scripts/**/*.js
172 files indexed
0 files ignored because of inclusion/exclusion patterns
Quality profile for cs: Sonar way
Quality profile for js: Sonar way
Excluded sources for coverage: 
  **/ProjectB/Views/**/*.cs
Sensor C# Properties [csharp]
Sensor C# Properties [csharp] (done) | time=15ms
Sensor SonarJavaXmlFileSensor [java]
Sensor SonarJavaXmlFileSensor [java] (done) | time=0ms
Sensor SonarJS [javascript]

我们没有尝试过的事情:

  • 通过更改项目文件排除单个文件
  • 我们不想这样做,因为它无法维护.

    We don't want to do this cause it's not maintainable.

    • 不包括整个项目

    我们只想从解决方案的单个项目中排除一些文件夹/样式.

    We only want to exclude some folders/patterns from single projects in the solution.

    推荐答案

    部分答案:可以在SonarQube UI中针对每个组件"(即,每个MSBuild项目)配置代码覆盖率排除范围,因此您应该可以排除 ProjectB \ Views 而不是分析.

    Partial answer: code coverage exclusions can be configured per "component" (i.e. per MSBuild project) in the SonarQube UI, so you should be able to exclude ProjectB\Views from coverage but not analysis.

    导航到组件页面,然后选择管理常规设置. 分析范围标签中的覆盖排除属性仅为此组件设置排除.

    Navigate to the component page, then select Administration, General Settings. The Coverage Exclusions property in the Analysis Scope tab sets the exclusions just for this component.

    查找组件页面并不明显:单击SonarQube项目的 Code 标签.这将显示组件列表.单击屏幕上您感兴趣的组件旁边的最左侧的图标:

    Finding the component page isn't obvious: click on the Code tab for the SonarQube project. That will show a list of the components. Click the left-most icon on the screen next to the component you are interested in:

    组件页面看起来与整个项目页面相同,但是页面顶部的路径将同时显示项目和组件名称(在下图中用红色下划线标记):

    The component page looks the same as the overall project page, but the path at the top of the page will show both the project and component name (underlined in red on the image below):

    这篇关于SonarQube MSBuild Scanner不会从分析中排除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆