Is there any way to disable certain metrics from selected packages in Sonar? I use Sonar to analyze my project and in Entity and DTO packages I have some code that is equal - the same field ID with annotations, etc is being reported as a duplication by Sonar. It has absolutely no sense to me so I'd like to disable it. How can I achieve this? Using the global exclusions option disables all metrics on selected package but how to do it just for code duplications?
You can exclude resources using the standard "sonar.exclusions" parameter or use the Switch Off violation plugin to exclude "Duplicated code" violations.
Note that the 2nd option (use of the switch off plugin) works only if you're using the SQALE plugin, which embeds the "sqale-java:DuplicatedBlocksCheck" rule.
With a newer SonarQube installation, you can use sonar.cpd.exclusions
to exclude certain files only from duplicate checks.
See: https://docs.sonarqube.org/latest/analysis/analysis-parameters/
Example:
sonar.cpd.exclusions=**/AssemblyInfo.cs,**/*.g.cs,**/Mappings/*.cs
sonar.cpd.exclusions
is shown in the SonarQube settings when you navigate to: Project settings > General Settings > Analysis Scope. –
Apeldoorn You can exclude resources using the standard "sonar.exclusions" parameter or use the Switch Off violation plugin to exclude "Duplicated code" violations.
Note that the 2nd option (use of the switch off plugin) works only if you're using the SQALE plugin, which embeds the "sqale-java:DuplicatedBlocksCheck" rule.
You can add these files to the properties in your pom.xml:
This one is to exclude from code coverage:
<sonar.coverage.exclusions>
your file paths
</sonar.coverage.exclusions>
This one is to exclude from code duplication:
<sonar.cpd.exclusions>
your file paths
</sonar.cpd.exclusions>
sonar
instead of sonarcube
;) –
Carbonari For me works its:
<sonar.cpd.exclusions>
com.simulate.java.dto\**
<\sonar.cpd.exclusions>
I have mult modules java projects just like that:
- parent
-- project-a
-- project-b
-- project-c
in the pom.xml of parent project inside of tag <properties>
i put:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<sonar.cpd.exclusions>
com.simulate.java.vo\**,
com.simulate.java.dto\**
<\sonar.cpd.exclusions>
<\properties>
Just like that. I hope I helped you.
If you are using Gradle, you can try adding this to your build.gradle file in your module:
def codeDuplicationExclusionList = [
// excluded packages
'**/com/yourpackage/domain/*',
'**/com/yourpackage/dto/*DTO.*',
]
sonarqube {
properties {
property 'sonar.cpd.exclusions', codeDuplicationExclusionList
}
}
© 2022 - 2025 — McMap. All rights reserved.