The PMD Plugin
The PMD plugin performs quality checks on your project’s Java source files using PMD and generates reports from these checks.
Usage
To use the PMD plugin, include the following in your build script:
plugins {
pmd
}
plugins {
id 'pmd'
}
The plugin adds a number of tasks to the project that perform the quality checks. You can execute the checks by running gradle check.
By default, PMD runs on the Java toolchain configured for the project, falling back to the Java version used to run Gradle when no toolchain is set. You can also run PMD with a specific JDK using a Java toolchain.
Supported PMD versions
Gradle supports PMD versions from 5.1.0 through 7.24.0.
Older PMD releases may work but are not tested against.
Use pmd.toolVersion to select a specific PMD version.
Tasks
The PMD plugin adds the following tasks to the project:
The PMD plugin adds the following dependencies to tasks defined by the Java plugin.
| Task name | Depends on |
|---|---|
|
All PMD tasks, including |
Dependency management
The PMD plugin adds the following dependency configurations:
| Name | Meaning |
|---|---|
|
The PMD libraries to use |
|
The additional libraries that are available for type resolution during analysis. This might be useful if PMD complains about missing classes. |
Configuration
pmd {
isConsoleOutput = true
toolVersion = "7.16.0"
rulesMinimumPriority = 5
ruleSets = listOf("category/java/errorprone.xml", "category/java/bestpractices.xml")
}
pmd {
consoleOutput = true
toolVersion = "7.16.0"
rulesMinimumPriority = 5
ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"]
}
See the PmdExtension class in the API documentation.
Configuring PMD with Java toolchains
By default, PMD analysis runs on the Java toolchain configured for the project, or on the JVM used to run Gradle when no toolchain is defined.
Using the javaLauncher property, you can run PMD tasks with a specific JDK version, independent of the JDK used to compile your project or to run Gradle.
The following configuration runs all PMD tasks with JDK 17:
tasks.withType(Pmd).configureEach {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(17)
}
}
Gradle provisions the requested JDK automatically through its toolchain support, so it does not need to be installed beforehand. Make sure the selected PMD version supports the chosen Java version.
Parallel analysis
You can configure the number of threads to be used by PMD for running its analysis.
pmd {
threads = 4
}
pmd {
threads = 4
}
|
This configuration is internal to PMD and is not linked to the number of workers used by Gradle.
It means that you have to pay attention to the value entered here and make sure it still makes sense in a multi project build.
This is because parallel Gradle task execution could result in different PMD tasks from different projects running in parallel.
If multiple PMD tasks execute simultaneously in n projects, then up to a maximum of (n * |