英文:
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…
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论