4

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

Depressio
  • 1,329
  • 2
  • 20
  • 39

1 Answers1

5

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.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 1
    It seems to work. I have to manually add the PMD dependency, too, but that's OK. Thus, I have a dependency like: `pmd fileTree(dir: 'build/extralib', includes: ['*.jar'])` and `pmd 'pmd:pmd:4.3'`. I got rid of the `pmdClasspath`, too. Seems to work. Thanks! – Depressio Jun 18 '13 at 22:02
  • Hi can you expand your answer, I struck with same issue. Could u please add your answer in details. – jcrshankar May 25 '21 at 00:07
  • if I add dependencies as `pmd ...` I get error as `java.lang.ClassNotFoundException: net.sourceforge.pmd.PMD` what can be wrong with it? – Swapnil Kotwal Feb 09 '23 at 13:00