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:

import de.johoop.cpd4sbt.CopyPasteDetector._

seq(cpdSettings : _*)

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

addSbtPlugin("de.johoop" % "cpd4sbt" % "1.1.2")

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

About Ruchi Agarwal

Software Consultant at Knoldus Software LLP having around 2 year experience working in Scala and java. She has good understanding of various technologies like LiftWeb, Akka, Amazon EC2, jQuery, javascript, CSS etc. She is RAD (Rational Application Developer) for WebSphere Software and RFT (Rational Funtional Tester) for Java certified. She is a skilled professional focused on the basic business functions and creation of software. She has a firm understanding of design methodology. She likes to work with responsibility, dedication and perseverance, she has a facility to establish good interpersonal relationships and foremost she is loyal to her principles, ethics and always giving to the best of her caliber.
This entry was posted in LiftWeb, Scala, Web and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s