Static code analysis via CPD from within SBT with Scala

CPD, SBT plug-in enables you to analyze your code with the help of the great PMD Copy/Paste Detector tool. It defines a cpd sbt action for that purpose. CPD used to detect duplicate code in the project.

Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse. It may be slightly faster to develop by duplicating code, because the developer need not concern himself with how the code is already used or how it may be used in the future. The difficulty is that original development is only a small fraction of a product’s life cycle, and with code duplication the maintenance costs are much higher. Some of the specific problems include:

  • Code bulk affects comprehension
  • Purpose masking
  • Update anomalies
  • File size

In this post, we are going to see how to integrate CPD with your Scala project

Integrate CPD plug-in with your sbt project as following:

1. For SBT 0.12, you’veĀ  to add the following to your project’s build.sbt file:

2. Add the plugin dependency to your project’s ./project/plugins.sbt or the global .sbt/project/build.sbt:

3. Run sbt command, then run cpd.

4. Now, a cpd.xml file would be generated in …/target/scala-2.10/cpd (default output path).

5. This cpd.xml file contains the all information about the duplicate code.

The settings specified with cpd are mostly valid, but they’re now specified using the new settings system of SBT 0.12.

Add the following to your project’s build.sbt file. Remember that all these settings should be added after the seq(cpdSettings : _*) declaration, so cpd pick up the new settings instead of default.

  • To change cpd report type (default – .XML)
cpdReportType := ReportType.{Simple, XML, CSV, VS}
  • To change cpd report name (default – “cpd.xml”):
cpdReportName := "cpd.xml"

You can find more about settings specified with cpd, here

Leave a Reply

%d bloggers like this: