8

I would like to use the gradle PMD plugin in an enterprise project which is built with gradle.

I have a pmd_rules.xml file which already works, but I can't add own java rules (I get a class not found exception). I followed the tutorial on it's website.

Where do I have to put my own rules so they get recognized by gradle and PMD? Has somebody already done something like that?

pmd.gradle:

apply from: rootProject.file("core/modules.gradle"), to : ext

if(project.name in (modules["modules"] +modules["modules"])){
    apply plugin: 'pmd'

    pmd {
        ignoreFailures = true
        ruleSetFiles = rootProject.files("../repo/pmd_rules.xml")
        sourceSets = [sourceSets.main,sourceSets.test]
        targetJdk = org.gradle.api.plugins.quality.TargetJdk.VERSION_1_7
        ruleSets = []
        toolVersion = "5.0.5"
    }
}
Opal
  • 81,889
  • 28
  • 189
  • 210
Manuel
  • 496
  • 6
  • 20

2 Answers2

3
tasks.withType(Pmd) {
    pmdClasspath += file("path/to/rules.jar")
}
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Peter's answer is perfectly correct: I asked the quesion also in the gradle forum: http://forums.gradle.org/gradle/topics/custom-rules-with-pmd-plugin. Instead of overriding the pmdClasspath, I now add the file to the classpath with the "+="-Operator – Manuel Oct 10 '14 at 13:26
0

For latest Gradle pmd plugin you need to do the following things:

  1. Add your library with pmd rules to plugin classpath https://github.com/dgroup/arch4u-pmd#gradle-buildgradle
    dependencies {
       ...
       pmd "io.github.groupid:libid:versionid"  
       pmd "commons-io:commons-io:2.11.0" 
       ...
    }
    
  2. Include ruleset from your lib https://github.com/dgroup/arch4u-pmd#include-arch4u-pmd-rules-into-your-existing-custom-ruleset
    <?xml version="1.0"?>
    <ruleset name="pmd ruleset with your rules">
    
      ...
      <rule ref="io/github/path-to-ruleset-from-your-lib.xml"/>
      ...
    
    </ruleset>
    
    or define particular rules in your https://github.com/dgroup/arch4u-pmd#reconfigure-a-rule
    <?xml version="1.0"?>
    <ruleset name="pmd ruleset with your rules">
    
      ...
      <!-- 2. Reconfigure rule with expected property -->
      <rule name="RuleFromYourLibrary" class="io.github.RuleFromYourLibrary">
        <priority>3</priority>
        <properties>
          ...
        </properties>
      </rule>
      ...
    
    </ruleset>
    
lazylead
  • 1,453
  • 1
  • 14
  • 26