Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use PMD's Copy/Paste Detector with Gradle

Tags:

gradle

ant

pmd

I'd like to use Copy/Paste Detector in my Gradle build.

This is why I've decided to translate the following Ant task (which I've found here) into Gradle syntax:

<target name="cpd">
    <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" />
    <cpd minimumTokenCount="100" outputFile="/home/tom/cpd.txt">
        <fileset dir="/home/tom/tmp/ant">
            <include name="**/*.java"/>
        </fileset>
    </cpd>
</target>

This is how the translation looks currently:

check << {
        ant.taskdef(name: 'cpd', classname: 'net.sourceforge.pmd.cpd.CPDTask', classpath: configurations.pmd.asPath)
        ant.cpd(minimumTokenCount: '100', outputFile: file('build/reports/pmd/copyPasteDetector.txt').toURI().toString()) {
            fileset(dir: 'src'){
                include(name: '**.java')
        }
    }
}

Unfortunately calling gradle check yields an net.sourceforge.pmd.cpd.ReportException, the stacktrace is here.

How can I scan my source code with the Copy/Paste Detector using Gradle 1.9?

Thanks!

like image 429
Matthias Braun Avatar asked Dec 26 '22 16:12

Matthias Braun


1 Answers

You can also use my gradle-cpd-plugin. See https://github.com/aaschmid/gradle-cpd-plugin for further informationen. Applying the cpd plugin automatically adds it the cpd as dependency of check task.

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

Currently, it is version 0.1 but I am on it to switch from using CPD's ant task internally to directly call it. This will include support of all parameters etc. Here is a 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')
}
like image 85
Andreas Schmid Avatar answered Dec 31 '22 12:12

Andreas Schmid