你可以申请一个使用project.afterEvaluate
的插件,然后以编程方式查找插件,如果它被应用,然后检查pmd
块并根据需要进行配置。如果插件未应用,则应用该插件并设置该块的默认值。
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 {}
然后你就可以看到所配置的PMD插件的结果
$ ./gradlew -b build_simple.gradle nothing -q
[java-basic]
[java-basic, basic, braces]
Now configured extension: [java-basic, basic, braces]