Depressio
Depressio

Reputation: 1379

Including jar in classpath when running Gradle's PMD plugin

I'm attempting to run PMD with a custom rule set file, but that rule set includes a rule that's a custom class. This class exists in a jar that is not pulled in as a dependency, but instead comes in a zip file (which is a dependency) and gets unpacked. Imagine the PMD rule class is just in build/extralib/blah.jar.

How do I include that in my classpath when running PMD only? What I've tried, but didn't work:

pmd {
    ruleSetFiles = files("build/utils/pmd-rules.xml")
    pmdClasspath = files("build/extralib")
}

To be clear, the error is: java.lang.ClassNotFoundException: com.package.for.pmd.CrazyRule. This happens when running pmdMain.

Secondary question: what's the difference between Pmd and PmdExtension? Pmd has pmdClasspath, but PmdExtension does not. When I added pmdClasspath, I got:

Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties. Deprecated dynamic property: "pmdClasspath" on "org.gradle.api.plugins.quality.PmdExtension_Decorated@70221fc5", value: "file collection".

So I guess it only adheres to PmdExtension? As a Gradle newbie, it's a bit confusing...

Upvotes: 4

Views: 1801

Answers (2)

SAM 77
SAM 77

Reputation: 1

All the rules needs to be inside a project and Gradle will build a jar and upload it to the artifactory for every project. Just include the below dependency in the Gradle pmd configurations and it will automatically pick the class files.

Example:

dependencies {
    pmd 'package:pmdcustomrules:develop'
}

Extract the jar to the build directory and set the XML path to ruleSetFiles.

pmd {
    ruleSetFiles = files("build/extractedFolder/pmd-rules.xml")
}

Upvotes: 0

Peter Niederwieser
Peter Niederwieser

Reputation: 123960

When you are configuring pmd { ... }, you are configuring the extension. Sometimes you may need to drop down to the task level and configure tasks.pmd { ... } instead. (Having an extension and a task of the same name is a common pattern used by the code quality and IDE extensions/tasks.) The easiest way to add stuff to the PMD class path is:

dependencies {
    pmd ...
}

I haven't tried if this works for adding external rules, but it might.

Upvotes: 5

Related Questions