Exclusion list for Duplicates on Quality Gate

I have a number of projects that follow a similar need to exclude a folder from the duplicate code metric on the Quality Gate.

I was thinking I could do this by creating a custom Quality Profile and adding the file pattern to the duplicates rule. The problem I find is there isn’t a Duplicate code rule so i can’t add it.

The Duplicate Code seems to be applied at the Quality Gate level and I don’t see any way to pass this file pattern in there.

Am I right in thinking I need to go to each project and add the exclusion in its sonar.properties file?

I did think I’d asked this previously but I can’t seem to find the thread, when i look at the projects that was for it looks like I created a custom Quality Gate that doesn’t enforce duplicates code. I’d prefer to avoid that sledge hammer approach and still get the duplicate warnings, just not on these files.

Hello @Paul_Birtle,

Good news, there’s a dedicated mechanism for this that doesn’t require touching the quality gate at all.

You can exclude specific files or folders from duplication detection using the sonar.cpd.exclusions property. This tells the scanner not to run duplicate analysis on the specified paths, so they won’t count against the duplication metric, while everything else continues to be checked as normal.

You can set it:

  • At analysis time (per project, in your pipeline): -Dsonar.cpd.exclusions=**/generated/**,**/vendor/**
  • In your project’s sonar-project.properties file: sonar.cpd.exclusions=**/generated/**
  • In SonarQube Cloud project settings under Administration, General Settings, Analysis Scope, Duplications, which avoids having to touch each project’s properties file

The last option (project settings UI) is probably what you’re after if you want to avoid per-project sonar.properties changes, though if you have many projects and want to set it consistently, the organization-level settings let you apply analysis scope settings across all projects at once.

See Excluding files from duplication detection for the full syntax and options.

Does that help?

Cheers,

Stevan

Thank you, yes that is what I was looking for, I was thinking it was in rules.

I will need to set it on many projects, but not all in the organisation. I think I’ll use the sonar.properties route and add it to our boilerplate project. This will mean manually changing each project if we ever need to change the value but I think that is unlikely.