How do I ignore duplicated code report in Sonar?

There are several ways for you to achieve this, depending on the importance you give to this duplication issue. SonarQube reports what it finds, it's all up to you to decide what to do with it.

  • If you believe that this is indeed an issue, you have to refactor your code: SonarQube cannot report duplication when there is none
  • If you believe that this particular instance is not an issue, you can either lower the severity of the issue, or mark it as "won't fix", with a nice comment for the person who will come after you - I believe that using @SuppressWarnings annotations for this is a bit of an abuse, when there are dedicated features in SonarQube
  • If you believe that SonarQube should not even raise issues about duplicated code, you can either disable the rule (the nuclear option), or setup your analysis to ignore duplication for your POJO package(s)

For instance, you can add the following property to your scanner configuration:

sonar.cpd.exclusions=path/to/your/package/*.java

The best way would be to tweak the quality gate to suit your need. Need to keep in mind that default built-in quality gate is standard and should be always preferred.

Copy the default and create a new quality gate. Edit it to accept duplication percentage according to your comfort level (depending on your project needs).

Copy the default and edit it to accept duplication percentage according to your comfort level

Tags:

Java

Sonarqube