Reporting on Duplicate Code
You should avoid code duplication like the plague! It's very insidious: you fix a bug and you think you're done. Unfortunately, this is not always true, as one developer may have copied/pasted in the past the buggy part you have just fixed. Fortunately, code duplication tools are available, and they have Maven plug-ins. Let's explore how to use the CPD (which stands for Copy Paste Detector) and Simian (http://www.redhillconsulting.com.au/products/simian/) tools.[3]
How do I do that?
CPD is actually part of the PMD project, and it's contained in the Maven PMD
plug-in. To use it, start by adding maven-pmd-plugin to the reports section of the POM. Then tell the
plug-in to generate the CPD report by adding the following property to
your project's project.properties
file:
maven.pmd.cpd.enable = true
Figure 4-7 shows a typical CPD report.

Figure 4-7. Typical CPD report showing duplicate lines
By default CPD reports duplicates that share more than 100
tokens. To configure it differently use the maven.pmd.cpd.minimumtokencount Maven
property. For example, to detect duplicates of 50 tokens or more, use
this:
maven.pmd.cpd.minimumtokencount = 50
Just as with code best practice detection, the duplicate reports are not very helpful when it comes to fixing the code. A better strategy is to set a high duplicate threshold and fail the build if duplicates are found. Then, as ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access