Disable Sonar duplications on Entity, DTO packages
Asked Answered
D

5

49

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?

Darleen answered 18/6, 2012 at 8:22 Comment(1)
I was also facing 'duplication' issue from sonar, the details helped to identify the issue easily.Ranket
S
7

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.

Sixtyfour answered 18/6, 2012 at 9:54 Comment(6)
I'm a little unsure how to use this Switch Off violation plugin with default sonar rules. What should it look like? com.projetc.dto.*;"Duplicated code";* com.projetc.dto.*;duplicated_lines;* What is the correct name of this rule?Darleen
I've updated my answer to be more precise about the use of the switch off violation plugin.Sixtyfour
Thanks for this. I think I am doing everything right, but it simply does not seem to work. When I try with simply putting: com.project.dto.*;*;* nothing changes. Do you have any ideas what might be the problem?Darleen
Removing the duplications themselves is not possible unless you excludes the corresponding files (with "sonar.exclusions"). Using the switch off violation plugin will juste "mute" the "sqale-java:DuplicatedBlocksCheck" violations (but I guess you're not using the SQALE plugin so you probably don't have such violations). In other words, the only solution is to exclude the files from the analysis.Sixtyfour
The Switch off violation plugin is now deprecated. The features have been incorporated into SorarQube itself.Savil
Hello. I configured "block exclusion" with begin/end patterns but it seems to have no effect on lines duplication analysis (whereas it works well for other violation). Is that behaviour expected ?Snicker
S
65

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
Squishy answered 8/3, 2017 at 7:55 Comment(4)
How can we do in the pom.xml file ?Earthshaker
This key isn't part of the documentation, unfortunately. However, the key sonar.cpd.exclusions is shown in the SonarQube settings when you navigate to: Project settings > General Settings > Analysis Scope.Apeldoorn
This is the correct answer for modern Sonar versions -- the accepted answer was the only workaround back in 2012. Anyone knows why the parameter is still not visible in the documentation?Carbonari
@Carbonari the parameter has been removed from the documentation from version 7.X to 8.X so it's definitely not "still not visible" in the documentation.Gaytan
S
7

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.

Sixtyfour answered 18/6, 2012 at 9:54 Comment(6)
I'm a little unsure how to use this Switch Off violation plugin with default sonar rules. What should it look like? com.projetc.dto.*;"Duplicated code";* com.projetc.dto.*;duplicated_lines;* What is the correct name of this rule?Darleen
I've updated my answer to be more precise about the use of the switch off violation plugin.Sixtyfour
Thanks for this. I think I am doing everything right, but it simply does not seem to work. When I try with simply putting: com.project.dto.*;*;* nothing changes. Do you have any ideas what might be the problem?Darleen
Removing the duplications themselves is not possible unless you excludes the corresponding files (with "sonar.exclusions"). Using the switch off violation plugin will juste "mute" the "sqale-java:DuplicatedBlocksCheck" violations (but I guess you're not using the SQALE plugin so you probably don't have such violations). In other words, the only solution is to exclude the files from the analysis.Sixtyfour
The Switch off violation plugin is now deprecated. The features have been incorporated into SorarQube itself.Savil
Hello. I configured "block exclusion" with begin/end patterns but it seems to have no effect on lines duplication analysis (whereas it works well for other violation). Is that behaviour expected ?Snicker
S
4

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>
Seiler answered 8/10, 2020 at 12:21 Comment(2)
I believe it's sonar instead of sonarcube ;)Carbonari
<properties> <sonar.coverage.exclusions> */component/, */constants/, */config/, */dao/.java, */service/.java, */abc/Main.java </sonar.coverage.exclnusions> <sonar.cpd.exclusions> **/vo/.java, */entity/.java </sonar.cpd.exclusions> </properties>Nevanevada
B
2

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.

Barberabarberry answered 22/10, 2021 at 1:51 Comment(2)
Hey, @rlresende. Any chance you can provide details?Arriviste
Hi @Arriviste of course. I described better my solution. I hope I helped you.Barberabarberry
K
0

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
  }
}
Kendo answered 14/5, 2024 at 12:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.