SonarQube中sonar-project.properties排除规则被忽略求助
问题背景
做POC验证时复制了部分C#文件,导致代码重复率过高,于是在代码库根目录添加sonar-project.properties文件,希望排除所有*Copy.cs文件的SonarQube重复代码检测。SonarScanner作为Azure Pipeline的一部分运行,日志显示已找到配置文件,但排除规则始终被忽略,疑似被覆盖。疑问:是否需要手动开启读取该配置文件的功能,还是添加后自动生效?
我的配置文件内容
sonar.projectKey=MyProjectName sonar.projectName=MyProjectName //tried with and without sonar.projectVersion=1.0 //tried with and without sonar.sourceEncoding=UTF-8 //tried with and without # Enable the Visual Studio bootstrapper sonar.visualstudio.enable=true //tried with and without sonar.sources=. //tried with and without # Duplication exclusions sonar.cpd.exclusions=**/*Copy.cs sonar.host.url=https://sonarqube.mydomain.com/ //tried with and without
Azure DevOps日志片段
2023-06-08T15:18:32.8604426Z INFO: Project root configuration file: D:\a\1\.sonarqube\out\sonar-project.properties 2023-06-08T15:18:33.1611676Z INFO: SonarScanner 4.8.0.2856 2023-06-08T15:18:33.1612569Z INFO: Java 11.0.19 Eclipse Adoptium (64-bit) 2023-06-08T15:18:33.1613584Z INFO: Windows Server 2022 10.0 amd64 2023-06-08T15:18:34.8396456Z INFO: User cache: C:\Users\VssAdministrator\.sonar\cache 2023-06-08T15:18:37.4347098Z INFO: Analyzing on SonarQube server 9.9.0.65466 2023-06-08T15:18:37.4359312Z INFO: Default locale: "en_US", source code encoding: "windows-1252" (analysis is platform dependent) 2023-06-08T15:18:38.9068139Z INFO: Load global settings 2023-06-08T15:18:39.1730647Z INFO: Load global settings (done) | time=281ms 2023-06-08T15:18:39.1815074Z INFO: Server id: B92B4E6C-AWu8amGVF99rjp3boDwD 2023-06-08T15:18:39.1972238Z INFO: User cache: C:\Users\VssAdministrator\.sonar\cache 2023-06-08T15:18:39.2010963Z INFO: Load/download plugins 2023-06-08T15:18:39.2011725Z INFO: Load plugins index 2023-06-08T15:18:39.3360359Z INFO: Load plugins index (done) | time=125ms 2023-06-08T15:18:45.6208126Z INFO: Load/download plugins (done) | time=6409ms 2023-06-08T15:18:46.7428102Z INFO: Loaded core extensions: developer-scanner 2023-06-08T15:18:48.0460701Z INFO: Process project properties 2023-06-08T15:18:48.1062471Z INFO: Process project properties (done) | time=63ms 2023-06-08T15:18:48.1084980Z INFO: Execute project builders 2023-06-08T15:18:48.2251995Z INFO: Execute project builders (done) | time=125ms 2023-06-08T15:18:48.2401855Z INFO: Project key: MyProjectName 2023-06-08T15:18:48.2402677Z INFO: Base dir: D:\a\1\s 2023-06-08T15:18:48.2403366Z INFO: Working dir: D:\a\1\.sonarqube\out\.sonar 2023-06-08T15:18:48.2554167Z INFO: Load project settings for component key: 'MyProjectName' 2023-06-08T15:18:48.3804222Z INFO: Load project settings for component key: 'MyProjectName' (done) | time=125ms
解决方案
- 修正配置文件注释格式:
sonar-project.properties仅支持#作为注释符号,你使用的//会被解析为属性值的一部分,导致配置项异常。把所有//开头的注释替换为#,修正后的配置示例:
sonar.projectKey=MyProjectName sonar.projectName=MyProjectName # tried with and without sonar.projectVersion=1.0 # tried with and without sonar.sourceEncoding=UTF-8 # tried with and without # Enable the Visual Studio bootstrapper sonar.visualstudio.enable=true # tried with and without sonar.sources=. # tried with and without # Duplication exclusions sonar.cpd.exclusions=**/*Copy.cs sonar.host.url=https://sonarqube.mydomain.com/ # tried with and without
确认配置文件位置:日志显示配置文件在
D:\a\1\.sonarqube\out\sonar-project.properties,但SonarScanner的Base dir是代码库根目录D:\a\1\s。确保你是将配置文件放在代码库根目录下,而非自动生成的.sonarqube/out目录,这样SonarScanner才能正确读取原始配置。无需手动开启配置文件读取:只要SonarScanner检测到
sonar-project.properties存在就会自动加载,日志里的Project root configuration file提示已经证明文件被加载了,问题出在配置格式而非加载开关。检查配置优先级:SonarQube的配置优先级为:服务器端项目配置 > Azure Pipeline任务参数 >
sonar-project.properties文件。检查SonarQube服务器端是否设置了重复代码排除规则,或者Pipeline任务中有没有覆盖sonar.cpd.exclusions的参数。验证路径匹配:确认
**/*Copy.cs的模式能匹配到目标文件,注意Windows环境下路径大小写不敏感,但尽量保证文件名后缀与模式一致。
内容的提问来源于stack exchange,提问作者Sam Jones

