Manuel
Manuel

Reputation: 506

Custom PMD rule with Gradle

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"
    }
}

Upvotes: 8

Views: 2168

Answers (2)

lazylead
lazylead

Reputation: 1989

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>
    

Upvotes: 0

Peter Niederwieser
Peter Niederwieser

Reputation: 123960

tasks.withType(Pmd) {
    pmdClasspath += file("path/to/rules.jar")
}

Upvotes: 3

Related Questions