43

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?

Arek
  • 1,941
  • 4
  • 22
  • 27
  • I was also facing 'duplication' issue from sonar, the details helped to identify the issue easily. – Sam May 07 '15 at 11:02

4 Answers4

58

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
fbastien
  • 762
  • 1
  • 9
  • 20
Roemer
  • 2,012
  • 1
  • 24
  • 26
  • 1
    How can we do in the pom.xml file ? – PAA Aug 31 '20 at 13:12
  • 4
    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. – Max Leske Apr 14 '21 at 12:34
  • 1
    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? – AdrienW Jul 02 '21 at 10:14
  • @AdrienW 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. – BlackEye Feb 22 '22 at 10:13
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.

codesalsa
  • 882
  • 5
  • 18
  • 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? – Arek Jun 18 '12 at 10:46
  • I've updated my answer to be more precise about the use of the switch off violation plugin. – Fabrice - SonarSource Team Jun 18 '12 at 11:59
  • 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? – Arek Jun 18 '12 at 12:57
  • 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. – Fabrice - SonarSource Team Jun 18 '12 at 13:33
  • The [Switch off violation plugin](http://docs.sonarqube.org/display/PLUG/Switch+Off+Violations+Plugin) is now deprecated. The features have been incorporated into SorarQube itself. – Matthew Strawbridge Sep 01 '15 at 10:01
  • 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 ? – Yves Martin Mar 14 '16 at 14:02
2

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>
Nitin
  • 2,701
  • 2
  • 30
  • 60
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.

rlresende
  • 59
  • 6