Gradle插件指定默认扩展名 [英] Gradle plugin specifying default extension values

查看:172
本文介绍了Gradle插件指定默认扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算编写包装几个代码质量gradle插件的自定义插件。逻辑很简单:使用这种自定义质量我想强制执行我们所有项目的默认标准。另一方面,我希望所有包装的插件仍然可以定制(例如,我想默认情况下设置PMD插件为基本规则集,但是我绝对不想限制任何人添加插件规则集)。

建议的级联扩展策略是什么?




  • 我应该为所有插件执行project.create({extension}),检查值是否设置并默认它们(和我应该如何区分默认插件扩展名和用户设置的默认值?)

  • 我应该创建myOwnExtension并设置这个自定义插件的扩展插件的值吗?
  • >
  • 是否有任何其他方式来自动执行级联?


谢谢!

解决方案

如果它被应用,然后检查 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?

  • Should I do project.create({extension}) for all the plugins, check values if the values are set and default them (and how would I distinguish default from plugin extension and the default value set by the user?)
  • Should I create myOwnExtension and set the values of the wrapped plugins extensions from this custom one?
  • Is there any other way how to automatically do the cascade?

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]

这篇关于Gradle插件指定默认扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆