I'm trying to exclude some c# files from duplicate code in Sonarqube by adding a new sonar-project.properties file to the root of my repository. I'm trying a "POC" where i've coped some C# files which are causing a high percentage of code duplication.
The sonar scanner runs as part of an azure pipeline
I've created the file, which looks like below:
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
My azure devops logs do seem to find the sonar-project.properties file:
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
The exclusions are ignored, it's as if the exclusions are overriden? Do i need to enable reading from the sonar-project.properties file or should it happen automatically when added?
thanks
The correct parameter to apply exclusions is sonar.exclusions. In essence, this parameter will ignore any file (or pattern of files) from being scanned.
You can put it on the config file like this:
Or call it from the cmd line as this:
Note that in order for exclusions to work correctly, you must: