TravisCI CircleCI Coverage Status codecov codebeat badge Maven Central Bintray license Issues Forks Stars

Gradle CPD plugin

Table of Contents

What is it

A Gradle plugin to find duplicate code using PMDs copy/paste detection (= CPD). See also https://plugins.gradle.org/plugin/de.aaschmid.cpd.

Requirements

Currently this plugin requires PMD greater or equal to version 6.1.0 such that toolVersion >= '6.1.0'.

Explanation: v2.0 removes deprecated rendering API (= net.sourceforge.pmd.cpd.Renderer) and replace it with new net.sourceforge.pmd.cpd.renderer.CPDRenderer which was introduced with v6.1.0, see PMD release notes.

Supported versions

G. CPD plugin Gradle PMD Java¹
v0.1 1.10 - 4.x 5.0.0 - 5.x 6 - 8
v0.2 2.0 - 4.x 5.0.0 - 5.x 6 - 8
v0.4 2.3 - 4.x 5.2.0 - 5.x 6 - 8
v1.0 2.14 - 5.0 5.2.0 - 5.x 6 - 8
v1.1 2.14 - 5.0 5.2.2 - 6.x 6 - 9
v1.2 >= 3.5.1 >= 5.2.2 >= 8
v1.3 >= 4.10.3 >= 5.2.2 >= 8
v2.0 4.10.3 - 5.5 >= 6.1.0 >= 8
v3.0 5.6 - 5.x >= 6.10.0 >= 8
v3.1 >= 5.6 >= 6.10.0 >= 8

¹: Java version may additionally depend on PMDs version which is might not be properly reflected here.

Usage

This plugin is available using either the new Gradle plugins DSL

plugins {
    id 'de.aaschmid.cpd' version '2.0'
}

or the old fashion buildscript block from Maven Central or jCenter.

buildscript {
    repositories {
        // choose your prefered one
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath 'de.aaschmid:gradle-cpd-plugin:2.0'
    }
}
apply plugin: 'cpd'

Attention: The plugins groupId was changed from de.aaschmid.gradle.plugins to de.aaschmid in v1.0.

By default the copy-paste-detection looks at all source code of all projects which at least apply LifecycleBasePlugin. If you use a different programming language and want to get it configured out of the box, please open an issue :-)

Single module project

If you have a single module project you just need to make sure that the LifecycleBasePlugin is also applied to it (explicitly or implicitly through e.g. Java or Groovy plugin). Otherwise you can simply add a dependency to a task you like by

analyze.dependsOn(cpdCheck)

Multi module project

If the root project of your multi-module project applies the LifecycleBasePlugin, you are done. But most likely this is not how your project looks like. If so, you need to manually add a task graph dependency manually to either one or all of your subprojects such that the cpdCheck task is executed:

// one single subproject where 'LifecycleBasePlugin' is available
subprojectOne.check.dependsOn(':cpdCheck')

// all subprojects where 'check' task is available (which comes with 'LifecycleBasePlugin')
subprojects {
    plugins.withType(LifecycleBasePlugin) { // <- just if 'LifecycleBasePlugin' plugin is not applied to all subprojects
        check.dependsOn(rootProject.cpdCheck)
    }
}

Custom sourceSets

If your are adding custom sourceSets (even in subProjects), it may occur that you either need to configure cpdCheck afterwards or even manually configure source. Unfortunately, I have had problems with this case in the CpdAcceptanceTest tests using Gradle TestKit.

Examples

This example shows a project where only main sources should be checked for duplicates:

// optional - settings for every CPD task
cpd {
    language = 'cpp'
    toolVersion = '6.1.0' // defaults to '6.13.0'; just available for v6.1.0 and higher (see explanation above)
}

// optional - default report is xml and default sources are 'main' and 'test'
cpdCheck {
    reports {
        text.enabled = true
        xml.enabled = false
    }
    source = sourceSets.main.allJava // only java, groovy and scala classes in 'main' sourceSets
}

Note: With v0.2, I have renamed the default task from cpd to cpdCheck that it does not have a name clash anymore.

Kotlin support

CPD supports Kotlin since v6.10.0. For previous versions you have to ignore files manually if you mixed it up with your Java files:

tasks.withType(Cpd) {
    exclude "*.kt"
}

Options

This plugin supports the following options, either set for the plugin using cpd { } or for every task explicitly, e.g. using cpdCheck { }:

Attribute Default Applies for language since
encoding System default v0.1
ignoreAnnotations false | 'java' v0.4
ignoreFailures false v0.1
ignoreIdentifiers false | 'java' v0.4
ignoreLiterals false | 'java' v0.4
language 'java' v0.4
minimumTokenCount 50 v0.1
skipDuplicateFiles false v0.5
skipLexicalErrors false v0.5
skipBlocks true | 'cpp' v0.4
skipBlocksPattern '#if 0\|#endif' | 'cpp' v0.4

If a specified language cannot be found, a fallback mechanism uses net.sourceforge.pmd.cpd.AnyLanguage instead. This fallback language does not run ANTLR and therefore also checks duplicates in comments.

For more information about options and their descriptions, see here, and for the available programming languages have a look on CPD documentation. To request more options, please file an issue here.

Additionally, one can configure the following reports for every task analogous to Reporting as for any other reporting plugin. See also the example in Usage section above.

Report Default since Further options and their defaults
csv disabled v0.1 separator = ',', includeLineCount = true²
text disabled v0.1 lineSeparator = '=====================================================================', trimLeadingCommonSourceWhitespaces = false
vs disabled v3.1 encoding = <<System default>>
xml enabled v0.1 encoding = <<System default>>

²: Since v3.1 but note that property includeLineCount is originally named lineCountPerFile and meaning is inverted which means that false shows line count and true hides it, see here.

Contributing

You are very welcome to contribute by providing a patch/pull request.

Please note that running the test cases my take quite long because the acceptance test cases (see de.aaschmid.gradle.plugins.cpd.test.CpdAcceptanceTest will download CPD and its dependencies for every version. I recommend to get these dependencies in your localMaven() repository as the test cases look there for it first.