-1

I want to set up PMD in my gradle project to have two rulesets, one that's mandatory and breaks builds, and one that just reports but lets the build continue. Is this possible?

Johnco
  • 4,037
  • 4
  • 34
  • 43

1 Answers1

0

Definitely possible!

By default the PMD plugin will setup 1 task to run for each sourceset. You can configure this for one of your 2 rulesests:

pmd {
  ignoreFailures = false
  ruleSets = [] // Remove defaults
  ruleSetFiles = files("path/to/mandaroty/ruleset.xml")
}

then, we need to setup a new set of independent tasks for the non-mandatory rules, something along the lines…

def sourceSets = convention.getPlugin(org.gradle.api.plugins.JavaPluginConvention).sourceSets
sourceSets.all { ss ->
  def taskName = ss.getTaskName('optionalPmd', null)
  project.tasks.register(taskName) { task ->
    task.description = "Run OPTIONAL PMD analysis for " + ss.name + " classes"
    task.source = ss.allJava
    task.conventionMapping.map("classpath", () -> ss.output.plus(ss.compileClasspath))
    task.ignoreFailures = true // override the value previously set
    task.ruleSetFiles = files("path/to/optional/ruleset.xml") // override the value previously set
  }

  // Have the optional analysis run during "check" phase
  project.tasks.named(org.gradle.api.plugins.JavaBasePlugin.CHECK_TASK_NAME) { checkTask ->
    checkTask.dependsOn taskName
  }
}

I'm writing this by heart, so some adjustments may be required…

Johnco
  • 4,037
  • 4
  • 34
  • 43