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

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

问题描述

我们目前正在使用 SonarQube Scanner for 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 构建任务以可维护的方式完成.

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)SourceProjectAScripts***.js"(类似于coverage)
    • 相对路径:**/ProjectsA/Scripts/**/*.js
  1. Excluding in the TFS Build Task
    • Absoulte path (with backslash or slash):sonar.exclusions="$(Build.SourcesDirectory)SourceProjectAScripts***.js" (similar for the coverage)
    • Relative path: **/ProjectsA/Scripts/**/*.js
  • 分析排除:**/ProjectA/Scripts/**/*.js
  • 覆盖排除:**/ProjectB/Views/**/*.cs
  • 不受支持并导致以下错误:sonar-project.properties 文件不被 SonarScanner for MSBuild 识别

我们看到的:

SonarQube 网页界面中 Scanner Context 的日志为:

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\_work5.sonarqubeout9
  - sonar.cs.analyzer.projectOutPaths="D:agent1\_work5.sonarqubeout9"
  - sonar.cs.roslyn.reportFilePath=D:agent1\_work5sSourceSolutionSourceProjectAinReleaseProjectA.dll.RoslynCA.json
  - sonar.cs.roslyn.reportFilePaths="D:agent1\_work5sSourceSolutionSourceProjectAinReleaseProjectA.dll.RoslynCA.json"
  - sonar.exclusions=**/ProjectA/Scripts/**/*.js
  - sonar.moduleKey=Solution:Solution:6FA7B5C2-667D-4387-98B9-445617F2AC0B
  - sonar.projectBaseDir=D:agent1\_work5sSourceSolutionSourceProjectA
  - sonar.projectKey=Solution:Solution:6FA7B5C2-667D-4387-98B9-445617F2AC0B
  - sonar.projectName=ProjectA
  - sonar.sourceEncoding=utf-8
  - sonar.sources="D:agent1\_work5sSourceSolutionSourceProjectAScriptsabc.js","..."

TFS 构建中 MSBuild Scanner 的日志为:

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

Base dir: D:agent1\_work5sSourceSolutionSourceProjectA
Working dir: D:agent1\_work5.sonarqubeout.sonarSolution_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 项目)配置代码覆盖排除,因此您应该能够排除 ProjectBViews 来自覆盖范围而非分析.

    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 ProjectBViews from coverage but not analysis.

    导航到组件页面,然后选择管理常规设置.Analysis Scope 选项卡中的 Coverage Exclusions 属性仅为该组件设置排除项.

    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天全站免登陆