A Gradle plugin to find duplicate code using PMDs copy/paste detection (= CPD). See also https://plugins.gradle.org/plugin/de.aaschmid.cpd.
This plugin requires PMD greater or equal to version 7.4.0 such that toolVersion >= '7.4.0'
.
You can use older versions of this plugin if you want to use older versions of PMD (see table below).
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 - 6.x | >= 8 |
v1.3 | >= 4.10.3 | 5.2.2 - 6.x | >= 8 |
v2.0 | 4.10.3 - 5.5 | 6.1.0 - 6.x | >= 8 |
v3.0 | 5.6 - 5.x | 6.10.0 - 6.x | >= 8 |
v3.1 | >= 5.6 | 6.10.0 - 6.x | >= 8 |
v3.2 | >= 6.6 | 6.10.0 - 6.x | >= 8 |
v3.3 | >= 6.6 | 6.10.0 - 6.x | >= 8 |
v3.4 | >= 7.4 | >= 7.0.0 | >= 8 |
v3.5 | >= 7.4 | >= 7.4.0 | >= 8 |
¹: Java version may additionally depend on PMDs version which is might not be properly reflected here.
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 :-)
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)
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)
}
}
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.
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.4.0' // defaults to '7.7.0'; just available for v7.4.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.
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) }
}
}
}
}
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"
}
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 | |
failOnError | true |
v3.5 | |
failOnViolation | true |
v3.5 | |
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, 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.
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.
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.