更新时间:2022-11-02 21:52:03
如果它被应用,然后检查 pmd
块并根据需要进行配置。如果该插件未应用,则应用该插件并设置该块的默认值。
apply plugin:groovy
group ='com.jbirdvegas.q41683529'
version ='0.1'
知识库{
jcenter()
}
class PmdWrapper实现插件< Project> {
@Override
void apply(Project target){
target.afterEvaluate {
def pmdPlugin = target.plugins.findPlugin(PmdPlugin)
//检查插件已应用,如果不应用
if(!pmdPlugin){
target.plugins.apply(PmdPlugin)
}
//获取扩展的引用并使用它操作值
println target.pmd.ruleSets
setValues(target.pmd as PmdExtension)
println target.pmd.ruleSets
println现在配置了规则集: $ {(target.pmd as PmdExtension).ruleSets}
}
}
static setValues(PmdExtension pmd){
//在这里您可以设置值或者根据需要添加或操作
if(!pmd.ruleSets.contains('basic')||!pmd.ruleSets.contains('braces')){
pmd.ruleSets<< 基本<< b
$ b $ {$ b $ $ b
然后您可以看到正在配置的pmd插件的结果
$ ./gradlew -b build_simple.gradle nothing -q
[java-basic]
[java-basic,basic,braces]
现在配置扩展:[java-basic,basic,braces]
I plan to write custom plugin wrapping several code quality gradle plugins. The logic is simple: using this custom quality I want to enforce "default" standards for all our projects. On the other hand, I want all the wrapped plugins still to be customizable (for example, I'd like to set PMD plugin with "basic" ruleset by default, but definitely I do not want to limit anyone to add addinitional rulesets).
What is the recommended strategy to cascade the extensions?
Thanks!
You could apply a plugin that uses project.afterEvaluate
then look for the plugin programmatically and if it's applied then check for the pmd
block and configure as needed. If the plugin is not applied then apply the plugin and set the defaults for the block.
apply plugin: "groovy"
group = 'com.jbirdvegas.q41683529'
version = '0.1'
repositories {
jcenter()
}
class PmdWrapper implements Plugin<Project> {
@Override
void apply(Project target) {
target.afterEvaluate {
def pmdPlugin = target.plugins.findPlugin(PmdPlugin)
// check if the plugin is already applied if not apply it
if (!pmdPlugin) {
target.plugins.apply(PmdPlugin)
}
// get a reference to the extension and use it to manipulate the values
println target.pmd.ruleSets
setValues(target.pmd as PmdExtension)
println target.pmd.ruleSets
println "Now configured ruleSets: ${(target.pmd as PmdExtension).ruleSets}"
}
}
static setValues(PmdExtension pmd) {
// here you can set the values or add or manipulate as needed
if (!pmd.ruleSets.contains('basic') || !pmd.ruleSets.contains('braces')) {
pmd.ruleSets << "basic" << "braces"
}
// blah for other values
}
}
apply plugin: PmdWrapper
task nothing {}
Then you can see the result of the pmd plugin being configured
$ ./gradlew -b build_simple.gradle nothing -q
[java-basic]
[java-basic, basic, braces]
Now configured extension: [java-basic, basic, braces]