[英]Gradle plugin extension set a property of type Map
I want to set a Map of attributes to my plugin extension. 我想为插件扩展设置属性映射。 So basically I want to write something like 所以基本上我想写点东西
settings {
envVars = {
a = "abc"
b = "dec"
...
n = "sdf"
}
}
When I use an attribute in my Extension class 当我在扩展类中使用属性时
private Map<?,?> envVars;
Gradle tells me that it can not set the property settings. Gradle告诉我无法设置属性设置。 So what I would like to achieve is to set a map of values in my extension class. 因此,我想实现的是在扩展类中设置值的映射。
What I did achieve is to get the closure when i write the following: 我做到的是在编写以下代码时获得闭包:
settings {
envVars {
a = "abc"
b = "dec"
...
n = "sdf"
}
}
public class extension {
....
public envVars(Closure c){}
}
But then I have no clue what to do with the closure and how to access what is inside, so I would rather have a Map instead of the closure 但是然后我不知道如何处理闭包以及如何访问内部内容,因此我宁愿使用Map而不是闭包
Regards Mathias 关于Mathias
Ok, you just have to write the map properly :/ 好的,您只需要正确编写地图即可:/
envVars = [test: 'test']
and everything is fine 一切都很好
I am using the following to read a map of values from build.gradle 我正在使用以下内容从build.gradle读取值映射
reference: https://github.com/liquibase/liquibase-gradle-plugin 参考: https : //github.com/liquibase/liquibase-gradle-plugin
Container class: 容器类:
class Database {
def name
def arguments = [logLevel: 'info']
Database(String name) {
this.name = name
}
Extension Class: 扩展类:
class MyExtension {
final NamedDomainObjectContainer<Database> databases
def databases(Closure closure){
databases.configure(closure)
}
def methodMissing(String name, args) {
arguments[name] = args[0]
}
}
Load Extensions 负载扩展
def databases = project.container(Database) { name ->
new Database(name)
}
project.configure(project) {
extensions.create("extensionName", MyExtension, databases)
}
Sample build.gradle: 示例build.gradle:
dbDiff{
databases{
db1{
url 'testUrl'
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.