我正在尝试编写自己的 gradle 插件,它需要能够配置一组对象 - 这些对象中有多少以及它们被称为什么取决于用户。
用于创建具有高级可定制性的自定义 gradle 插件的文档非常差。它提到project.container()
了做这种事情的方法,但我不知道如何让它在我的用例中工作。
这是我的插件配置 DSL 的一个示例:
teregrin {
terraformVersion = '0.6.6'
root("dev"){
accessKey = "flobble"
}
root("prd"){
}
}
这是我的插件扩展对象,允许我对其进行配置:
class TeregrinPluginExtension {
boolean debug = false
boolean forceUnzip = false
String terraformVersion = null
Set<TeregrinRoot> roots = []
def root(String name, Closure c){
def newRoot = new TeregrinRoot(name)
c.setDelegate(newRoot)
c()
roots << newRoot
}
}
扩展以标准方式连接到我的插件中:
project.extensions.create("teregrin", TeregrinPluginExtension)
这工作正常,但它是一种非常丑陋的配置风格,并不是典型的 gradle DSL 的风格。
如何将我的插件配置 DSL 更改为如下所示:
teregrin {
terraformVersion = '0.6.6'
roots {
dev {
accessKey = "flobble"
}
prd {
}
}
}