PMD plugin reporting violation of unconfigured rule with Gradle 5

We’re in the exciting process of upgrading our build to Gradle 5. One issue is that PMD doesn’t seem to be working as before. It reports a violation to a rule that is not even included in the configuration file.

We’ve got a multi-module build. What we’re doing so far is exporting the Checkstyle, PMD and Spotbugs configurations from our Sonarqube instance and using these during the Gradle build. For the export, we’re using the excellent Gradle Download Task.

Sorry that I can’t post the whole code here, but I’ll try to explain our setup as good as I can.

build.gradle:

allprojects {
apply plugin: "idea"
apply plugin: "eclipse"
apply plugin: "checkstyle"
apply plugin: "pmd"
apply plugin: "com.github.spotbugs"
apply from: "${rootDir}/quality-download.gradle"
apply plugin: "io.spring.dependency-management"
apply plugin: "org.owasp.dependencycheck"
...
}
...
subprojects {
...
pmd {
    sourceSets = [sourceSets.main]
    ignoreFailures = false
    ruleSetFiles = files(downloadPMDConfig.dest)
    toolVersion = "6.10.0"
}
...
tasks.pmdMain.dependsOn(downloadPMDConfig)
}

quality-download.gradle:

def checkstyleConfig = "${rootDir}/config/checkstyle/checkstyle.xml"
def checkstyleURL = "<top-secret-sonarqube-url>"
def pmdConfig = "${rootDir}/config/pmd/pmd.xml"
def pmdConfigURL = "<top-secret-sonarqube-url>
def findbugsConfig = "${rootDir}/config/findbugs/findbugs.xml"
def findbugsConfigURL = "<top-secret-sonarqube-url"

task downloadCheckstyleConfig(type: Download) {
src checkstyleURL
dest file(checkstyleConfig)
overwrite false
}

task downloadPMDConfig(type: Download) {
src pmdConfigURL
dest file(pmdConfig)
overwrite false
}

task downloadFindbugsConfig(type: Download) {
src findbugsConfigURL
dest file(findbugsConfig)
overwrite false
}

We can tell that the plugin picks up the configuration file, because we see warnings of this type in the console:

Use Rule name category/java/bestpractices.xml/PositionLiteralsFirstInComparisons instead of the deprecated Rule name rulesets/java/design.xml/PositionLiteralsFirstInComparisons. PMD 7.0.0 will remove support for this deprecated Rule name usage.

We see this exactly twice for every rule in the file, so that’s a pretty good indicator that PMD is using the file during the gradle run.

However, the build fails with a PMD error related to a rule that is not in the PMD configuration file.

What could be going on here?

There are multiple sources for rules that factor in to the PMD run. They are ruleSets, ruleSetConfig, and ruleSetFiles. Your error that is not in the config file can come from the default ruleSets. Set those to empty if you only care about the rules in your file. You can optionally replace ruleSetFiles with ruleSetConfig, which is a newer API (but shouldn’t change your results).

pmd {
    sourceSets = [sourceSets.main]
    ignoreFailures = false
    ruleSetConfig = resources.text.fromFile(downloadPMDConfig.dest)
    ruleSets = []
    toolVersion = "6.10.0"
}

Thank you very much, setting the ruleSets explicitly empty did the trick.