The Gradle Build Tool provides a PMD Plugin that can be added to your build configuration. Technically it is based on the Ant Task.

Example

In your build.gradle add the following:

plugins {
    id 'pmd'
}

Custom ruleset

Configuration of a custom ruleset looks like this:

pmd {
    ruleSetFiles = files("custom-pmd-ruleset.xml")
    ruleSets = []
}

Note: The ruleSets array is explicitly set to empty to avoid using the default configuration.

Fail the build

If you want to fail the build for pmd violations, you need to set ignoreFailures:

pmd {
    ignoreFailures = false
}

More configuration options are documented on PMD Extension.

Upgrade PMD version

If you want to use a newer PMD version than the default one provided with gradle, you can do so with the property toolVersion:

pmd {
    toolVersion = "6.21.0"
}

References

Source code for Gradles PMD Plugin is available here:

Tags: userdocs tools