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

Leave a Comment