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?
By default, SonarQube detects duplications at 4 levels:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With