Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube is adding duplications when code is repeated in other projects besides the one under analysis

I have a set of projects the have lot of code in common. SonarQube displays a high percentage of duplication. When I go to see the duplication per file, the references on the duplicated code point to the other projects code where the code is the same. Is there a way to have duplication only run against the same project?

like image 359
Ovidio Pozo Avatar asked Sep 01 '16 17:09

Ovidio Pozo


2 Answers

By default, SonarQube detects duplications at 4 levels:

  • Within a source file
  • Across multiple files in a project
  • Across modules of a project
  • Across multiple projects

You can turn the last one off globally or at the project level: Administration > General > Duplications > Cross project duplication detection

However, you might not want to do that. The point of detecting cross-project duplications is to help you recognize opportunities to pull shared code out into libraries. Because after all duplications turn into a maintenance nightmare: when a change is needed, you have to make it n times in n places, and change n tests.

like image 188
G. Ann - SonarSource Team Avatar answered Sep 30 '22 17:09

G. Ann - SonarSource Team


Even though the answer given by G. Ann is quite sufficient for this question but still i want to add a valid scenario where we could need to ignore duplicate code to detect by Sonar. We can have a scenario where we are having multiple Java Exception files in different-different projects under single microservice. If we are not going to write the jUnit test cases for these Exceptions then we have to exclude these duplicate files to be detected by Sonar using below configuration in pom.xml file.

<sonar.cpd.exclusions> **/Exception.java </sonar.cpd.exclusions>

It's just an example which can have a different structure in different projects.

like image 28
Ash Avatar answered Sep 30 '22 16:09

Ash