【发布时间】:2018-09-23 18:58:44
【问题描述】:
我们目前正在为 VSTS/TFS 4.1.1 使用 SonarQube Scanner(它正在使用 SonarQube Scanner 4.1.1)。
我们想做的事:
对于我们解决方案中的某些项目(模块),我们希望从分析和代码覆盖率统计中排除文件。这应该通过使用文件模式而不是修改 TFS 构建任务以可维护的方式完成。
项目结构:
|- Source
|- ProjectA
|- Scripts (should be excluded)
|- OwnCode
|- ProjectB
|- Views (only code coverage should be excluded)
|- Presenters
|- ...
|- ProjectC
|- Scripts (should be scanned)
|- ...
|- ...
|- Solution.sln
我们尝试了什么:
- 在 TFS 构建任务中排除
- 绝对路径(带反斜杠或斜杠):
sonar.exclusions="$(Build.SourcesDirectory)\Source\ProjectA\Scripts\**\*.js"(与覆盖范围类似) - 相对路径:
**/ProjectsA/Scripts/**/*.js
- 绝对路径(带反斜杠或斜杠):
- 在 SonarQube 前端中排除
- 分析排除:
**/ProjectA/Scripts/**/*.js - 覆盖排除:
**/ProjectB/Views/**/*.cs
- 分析排除:
- 不包括 sonar-project.properties:
- 不支持并导致以下错误:
sonar-project.properties files are not understood by the SonarScanner for MSBuild
- 不支持并导致以下错误:
我们所看到的:
SonarQube 网页界面中 Scanner Context 的日志为:
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 Scanner 的日志为:
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]
我们没有尝试的:
- 通过更改项目文件排除单个文件
我们不想这样做,因为它不可维护。
- 排除整个项目
我们只想从解决方案中的单个项目中排除一些文件夹/模式。
【问题讨论】:
-
此扫描仪的主要问题在于基本路径。正如您在此处看到的:
Base dir: D:\agent1\_work\5\s\Source\Solution\Source\ProjectA它包含 ProjectA,因此您的过滤器**/ProjectA/Scripts/**/*.js将不起作用。它应该类似于**/Scripts/**/*.js。但是,此过滤器将从所有项目中删除所有Scripts,这不是您想要的。 Ants 通配符也不接受..,因此您无法向上移动目录。我有一个类似的问题,我最终删除了我在分析中不想要的文件。我会投赞成票,因为我也对答案感兴趣。 -
这不适用于 CS 文件,因为构建会中断。我想知道为什么没有简单的解决方案。有些人通过创建 hackish 解决方案来解决这个问题,例如:stackoverflow.com/a/48705850/2560519
-
我不会破坏构建,如果你这样运行它: 1.
SonarQube.Scanner.MSBuild.exe begin2.MSBuild.exe3. 删除文件 4.SonarQube.Scanner.MSBuild.exe end.它会给你关于 SonarQube 中丢失文件的警告,但除此之外,一切都会好起来的。但就像我说的,这是一个奇怪的解决方法。我也对永久解决方案感兴趣。
标签: .net tfs msbuild sonarqube sonarqube-scan