Home

Awesome

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

This plugin requires PMD greater or equal to version 7.0.0 such that toolVersion >= '7.0.0'.

Explanation: v7.0.0 changed the internal API, see PMD release notes.

You can use older versions of this plugin if you want to use older versions of PMD (see table below).

Supported versions

G. CPD pluginGradlePMDJava¹
v0.11.10 - 4.x5.0.0 - 5.x6 - 8
v0.22.0 - 4.x5.0.0 - 5.x6 - 8
v0.42.3 - 4.x5.2.0 - 5.x6 - 8
v1.02.14 - 5.05.2.0 - 5.x6 - 8
v1.12.14 - 5.05.2.2 - 6.x6 - 9
v1.2>= 3.5.15.2.2 - 6.x>= 8
v1.3>= 4.10.35.2.2 - 6.x>= 8
v2.04.10.3 - 5.56.1.0 - 6.x>= 8
v3.05.6 - 5.x6.10.0 - 6.x>= 8
v3.1>= 5.66.10.0 - 6.x>= 8
v3.2>= 6.66.10.0 - 6.x>= 8
v3.3>= 6.66.10.0 - 6.x>= 8
v3.4>= 7.4>= 7.0.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 '3.4'
}

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

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

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

Attention: The plugin's 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 you 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 = '7.0.0' // defaults to '7.2.0'; just available for v7.0.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 so that it does not have a name clash anymore.

Property source

Depending on your needs, java sourcesets of all projects will be automatically added to source and therefore checked for duplicates (see Code). You have a lot of options for overriding this behaviour but note that the lazy evaluation - especially later configured subprojects - could add further sourceSets again unless you use [evaluationDependsOnChildren()](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#evaluationDependsOnChildren--) for your rootProject:

source = subprojects*.sourceSets*.main*.java*.srcDirs

or

source = subprojects*.sourceSets*.main*.java*.srcDirs + subprojects*.sourceSets*.test*.java*.srcDirs

or

cpdCheck {
    source = []
    allprojects.forEach { project ->
        project.plugins.withType(JavaPlugin) { plugin ->
            project.sourceSets.main.java.forEach { s -> rootProject.cpdCheck.source(s) }
        }
    }
}

or same using kotlin DSL (Note: That works only if all subprojects have sourceSets main and test e.g. by appling the java plugins. Otherwise, you can just filter for JavaBasePlugin before)

setSource(files(
    // only check java source code
    subprojects.flatMap { it.the<SourceSetContainer>()["main"].java.srcDirs },
    subprojects.flatMap { it.the<SourceSetContainer>()["test"].java.srcDirs }
))

or if you need a new cpd task for kotlin and not all subprojects apply a java plugin (see also here)

tasks.register<Cpd>("cpdKotlin") {
    language = "kotlin"
    exclude { it.file.extension.contains("java") }

    allprojects.forEach { project ->
        project.plugins.withType<JavaPlugin> {
            project.convention.getPlugin<JavaPluginConvention>().sourceSets.configureEach {
                allJava.srcDirTrees.forEach { this@register.source(it) }
            }
        }
    }
}

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 { }:

AttributeDefaultApplies for languagesince
encodingSystem defaultv0.1
ignoreAnnotationsfalse'java'v0.4
ignoreFailuresfalsev0.1
ignoreIdentifiersfalse'java'v0.4
ignoreLiteralsfalse'java'v0.4
language'java'v0.4
minimumTokenCount50v0.1
skipDuplicateFilesfalsev0.5
skipLexicalErrorsfalsev0.5
skipBlockstrue'cpp'v0.4
skipBlocksPattern'#if 0|#endif''cpp'v0.4

If a specified language cannot be found, analysis fails.

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.

ReportDefaultsinceFurther options and their defaults
csvdisabledv0.1separator = ',', includeLineCount = true²
textdisabledv0.1lineSeparator = '=====================================================================', trimLeadingCommonSourceWhitespaces = false
vsdisabledv3.1encoding = <<System default>>
xmlenabledv0.1encoding = <<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.