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

Experienced working in Scala/ Java as a Sr. Software Consultant at Knoldus Software LLP for around 4 years. Having good understanding of various technologies includes Play, AngularJS, MongoDB, LiftWeb, Akka, jQuery, Javascript, CSS etc. Certified in RAD (Rational Application Developer) for WebSphere Software and RFT (Rational Funtional Tester) for Java. Skilled professional focused on the basic business functions and creation of software. Having firm understanding of design methodology. Likes to work with responsibility, dedication and perseverance.
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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s