两层规则集在PMD Gradle插件中。

huangapple go评论75阅读模式
英文:

Two-tiered rulesets in PMD Gradle plugin

问题

我想在我的Gradle项目中设置PMD,以拥有两个规则集,一个是强制执行的,并且会中断构建,另一个只是生成报告,但允许构建继续。这可能吗?

英文:

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?

答案1

得分: 0

当然可以!

默认情况下,PMD插件会为每个源集设置一个任务来运行。您可以为其中的一个规则集进行配置:

pmd {
  ignoreFailures = false
  ruleSets = [] // 移除默认规则集
  ruleSetFiles = files("path/to/mandaroty/ruleset.xml")
}

然后,我们需要为非强制规则设置一组新的独立任务,大致如下:

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 = "为 " + ss.name + " 类运行可选的PMD分析"
    task.source = ss.allJava
    task.conventionMapping.map("classpath", { ss.output.plus(ss.compileClasspath) })
    task.ignoreFailures = true // 覆盖之前设置的值
    task.ruleSetFiles = files("path/to/optional/ruleset.xml") // 覆盖之前设置的值
  }

  // 在“check”阶段运行可选分析
  project.tasks.named(org.gradle.api.plugins.JavaBasePlugin.CHECK_TASK_NAME) { checkTask ->
    checkTask.dependsOn taskName
  }
}

由于我是凭记忆编写的,可能需要进行一些调整...

英文:

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…

huangapple
  • 本文由 发表于 2020年8月28日 00:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63620139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定