CPD support


(Chris Jones) #1

It’s awesome that Gradle now has built-in support for PMD, but I don’t see any CPD support bundled with that. Since CPD is part of PMD, that lack surprises me. Am I not looking in the right place?


(Peter Niederwieser) #2

The Gradle PMD plugin builds on the PMD Ant task. As far as I can tell, CPD is a separate tool with a separate Ant task (even though it’s part of the PMD project). In that case, we don’t support it yet.


(Chris Jones) #3

If/when you do add support for CPD, one important thing to note: PMD can very reasonably be run separately for each subproject; but CPD should be run once across the union of all known source sets. Here’s some code I have that does that, which you’re welcome to build on:

task cpd(dependsOn: ':pmdSetup') {
    // Combine all source sets
    allSource = files {
        allprojects.findAll { proj ->
            proj.hasProperty("sourceSets")
        }.collect { proj ->
            proj.sourceSets.collect { ss ->
                ss.java
            }
        }
    }
      // Declare this task's inputs and outputs.
    inputs.files allSource
    outDir = file("$buildDirName/cpd")
    outputs.dir outDir
    //
  outputs.files file("$outDir.path/cpd.xml")
      doLast {
        outDir.mkdirs()
        // Keep a reference to the gradle project for use inside the
        // ant closure, where "project" refers to the ant project.
        gproj = project
        ant {
            cpd(minimumTokenCount: '100', format: 'xml',
                    outputFile: outDir.path + '/cpd.xml') {
                fileset(dir: projectDir.getPath()) {
                    // Convert the gradle sourceSet to an ant
                    // fileset.
                    allSource.each { file ->
                        include(name: gproj.relativePath(file))
                    }
                }
            }
        }
    }
}

(Muneer Basha) #4

Please Visit

http://www.javavillage.in for more details on PMD & CPD


(Torben Vesterager) #5

Yep, let’s get CPD working with gradle, so we can get some meaningful info in the Jenkins DRY plugin


(mareser) #6

It took me some time to get CPD running using Gradle; here are my findings: http://stackoverflow.com/questions/20447408/use-pmds-copy-paste-detector-with-gradle/20776993#20776993


(Debug abc) #7

Hi, We are using CPD tool for code duplication detection. CPD tool includes whitespaces and comments. Could you please let us know how we can avoide white spaces, comments so that correct cases of duplicity can come?

Suppose we have 4 lines of duplicate code and 4 lines of comments then it returns 8 lines instead of 4.


(Debug abc) #8

Hi, We are using CPD tool for code duplication detection. CPD tool includes whitespaces and comments. Could you please let us know how we can avoide white spaces, comments so that correct cases of duplicity can come?

Suppose we have 4 lines of duplicate code and 4 lines of comments then it returns 8 lines instead of 4.


(Andreas Schmid) #9

As using CPD with Gradle is a lot of code I have written a gradle-cpd-plugin. See https://github.com/aaschmid/gradle-cpd-plugin for further informationen. Currently, it is version 0.1 but I am on it to switch from using CPD’s ant task to directly call it. This will provide support of all parameters etc.

Note: I am not very happy with the name “cpd” for extension (see toolVersion) and task, suggestions welcome :wink:

Usage example:

apply plugin: 'cpd'
  buildscript {
    repositories {
        mavenCentral()
    }
      dependencies {
        classpath 'de.aaschmid.gradle.plugins:gradle-cpd-plugin:0.1'
    }
}
  // optional - default is 5.1.0
cpd {
    toolVersion = '5.0.5'
}
  tasks.cpd {
    reports {
        text.enabled = true
        xml.enabled = false
    }
    source = files('src/main/java')
}