GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PMD Source Code Analyzer Project
  • Nav
  • Download
  • GitHub
  • Edit on GitHub

This view

  • Original page
  • New address

GeezerVison is showing docs.pmd-code.org — Gradle | PMD Source Code Analyzer

 
  • PMD 7.31.0-SNAPSHOT
  • Release date: 28-December-2026
  • About
    • Home
    • Release notes
    • Getting help
    • Release policies
    • Support lifecycle
    • Security
  • User Documentation
    • Migration Guide for PMD 7
    • Installation and basic CLI usage
    • Making rulesets
    • Configuring rules
    • Best practices
    • Suppressing warnings
    • Incremental analysis
    • PMD CLI reference
    • PMD Report formats
    • 3rd party rulesets
    • Signed Releases
    • CPD reference
      • Copy-paste detection
      • CPD Report formats
    • Extending PMD
      • Introduction to writing rules
      • Your first rule
      • XPath rules
      • Java rules
      • Rule designer reference
      • Defining rule properties
      • Rule guidelines
      • Testing your rules
      • Creating (XML) dump of the AST
    • Tools / Integrations
      • Maven PMD Plugin
      • Gradle
      • Ant
      • PMD Java API
      • bld PMD Extension
      • CI integrations
      • IDE Plugins
      • Other Tools / Integrations
  • Rule Reference
    • Apex Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Documentation
      • Error Prone
      • Performance
      • Security
    • HTML Rules
      • Index
      • Best Practices
    • Java Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Documentation
      • Error Prone
      • Multithreading
      • Performance
      • Security
    • Java Server Pages Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Error Prone
      • Security
    • JavaScript Rules
      • Index
      • Best Practices
      • Code Style
      • Error Prone
      • Performance
    • Kotlin Rules
      • Index
      • Best Practices
      • Error Prone
    • Maven POM Rules
      • Index
      • Error Prone
    • Modelica Rules
      • Index
      • Best Practices
    • PLSQL Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Error Prone
    • Salesforce Visualforce Rules
      • Index
      • Security
    • Scala Rules
      • Index
    • Swift Rules
      • Index
      • Best Practices
      • Error Prone
    • Velocity Template Language (VTL) Rules
      • Index
      • Best Practices
      • Design
      • Error Prone
    • WSDL Rules
      • Index
    • XML Rules
      • Index
      • Best Practices
      • Error Prone
    • XSL Rules
      • Index
      • Code Style
      • Performance
  • Language-Specific Documentation
    • Overview
    • Language configuration
    • Apex
    • C/C++
    • C#
    • CSS
    • Coco
    • Dart
    • Fortran
    • Gherkin
    • Go
    • HTML
    • Java
    • JavaScript / TypeScript
    • JSP
    • Julia
    • Kotlin
    • Lua
    • Matlab
    • Modelica
    • Objective-C
    • Perl
    • PHP
    • PLSQL
    • Python
    • Ruby
    • Rust
    • Scala
    • Swift
    • T-SQL
    • Visualforce
    • Velocity Template Language (VTL)
    • XML and XML dialects
  • Developer Documentation
    • Contributing
      • Contributing
      • Developer resources
      • Newcomers' Guide
      • Writing documentation
    • Building PMD
      • General Info
      • Building PMD from source
      • Building PMD with IntelliJ IDEA
      • Building PMD with Eclipse IDE
      • Building PMD with VS Code IDE
      • Building PMD with Netbeans IDE
    • Roadmap
    • GitHub Actions Workflows
    • How PMD works
    • Logging
    • Pmdtester
    • Rule Deprecation Policy
    • Major contributions
      • Rule Guidelines
      • Adding a new dialect
      • Adding a new language (JavaCC)
      • Adding a new language (ANTLR)
      • Adding a new CPD language
    • Experimental features
      • List of experimental Features
  • Project documentation
    • Trivia about PMD
      • PMD in the press
      • Products & books related to PMD
      • Similar projects
      • What does 'PMD' mean?
    • Logo
    • FAQ
    • License
    • Credits
    • Old Release Notes
      • Release Notes PMD 7.x
      • Detailed Release Notes PMD 7.0.0
      • Release Notes PMD 6.x
      • Release Notes PMD 5.x
      • Release Notes PMD 4.x and earlier
    • Decisions
    • Project management
      • Infrastructure
      • Release process
      • Merging pull requests
      • Main Landing page

Gradle

Table of Contents

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 = "7.31.0-SNAPSHOT"
}

Note

For PMD 7, at least Gradle 8.6 is needed. See Support for PMD 7.0.

Aux Classpath

Gradle by default adds all project dependencies to the aux classpath. Since PMD 7.27.0, you’ll get a warning if the platform classes will be resolved by the running JVM. To avoid this, explicitly add the platform classpath of the correct Java version to PMD’s aux classpath. The example is using the toolchains feature of Gradle:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

// Add jrt-fs.jar of the toolchain explicitly on pmdAux classpath, to avoid
// warning "Adding current platform ... to auxClasspath, which could be the wrong java version."
dependencies {
    pmdAux javaToolchains.launcherFor(java.toolchain)
            .map { it.metadata.installationPath }
            .map { it.files("lib/jrt-fs.jar")}
}

See Providing the auxiliary classpath.

Environment Variables

Environment variables are not automatically passed through to Gradle’s PMD Plugin. E.g. using any Language properties with Gradle is not straight forward, as PMD is called in an isolated worker process without any environment variables.

The following snippets creates a subclass of Gradle’s PMD task in order to provide all PMD specific environment variables. This task needs to be registered and the default tasks “pmdMain” and “pmdTest” need to be disabled.

abstract class PmdWithEnv extends Pmd {
    @Input
    abstract MapProperty<String, String> getPmdEnvironment()

    @Override
    protected void configureForkOptions(JavaForkOptions forkOptions) {
        super.configureForkOptions(forkOptions)
        forkOptions.environment(pmdEnvironment.get())
    }
}

tasks.named('pmdMain') {
    enabled = false
}
tasks.register('pmdMainEnv', PmdWithEnv) {
    source = pmdMain.source
    classpath = pmdMain.classpath
    pmdEnvironment.convention(providers.environmentVariablesPrefixedBy('PMD_'))
}

tasks.named('pmdTest') {
    enabled = false
}
tasks.register('pmdTestEnv', PmdWithEnv) {
    source = pmdTest.source
    classpath = pmdTest.classpath
    pmdEnvironment.convention(providers.environmentVariablesPrefixedBy('PMD_'))
}

tasks.named('check') {
    dependsOn tasks.withType(PmdWithEnv)
}

With that configuration, you can call Gradle like that:

PMD_JAVA_DISABLE_AUX_CLASSPATH_WARNINGS=true ./gradlew check

Complete Example

See https://github.com/pmd/pmd-examples/tree/main/gradle/simple-project.

References

Source code for Gradle’s PMD Plugin is available here:

  • gradle/gradle code-quality
    • Pmd.java
    • PmdExtension.java
    • PmdPlugin.java
    • PmdInvoker.java
  • The default PMD version used by Gradle 9.7.1: DEFAULT_PMD_VERSION
Tags: userdocs tools
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.