Search code examples
gradlepmd

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...


Solution

  • 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.