在 Gradle 中,我们可以像下面这样定义和访问变量吗?在范围(命名空间)内定义变量的更方便的方法。
foo {
bar1 {
variable1 = 'hello';
variable2 = 'hello2';
}
bar2 {
variable1 = 'hello';
variable2 = 'hello2';
}
}
println foo.bar1.variable1
println foo.bar2.variable2
或者需要 map
foo = [bar1 : [variable1 : 'hello',
variable2 : 'hello2'
],
bar2 : [variable1 : 'hello',
variable2 : 'hello2'
]
]
最佳答案
您应该使用 ConfigSlurper
在 foo.groovy
中设置您的变量
foo {
bar1 {
variable1 = 'hello'
variable2 = 'hello2'
}
bar2 {
variable1 = 'hello'
variable2 = 'hello2'
}
}
然后加载它
ConfigSlurper foo = new ConfigSlurper().parse(new File('/path/to/foo.groovy').toURI().toURL())
println foo.bar1.variable1
println foo.bar2.variable2
关于variables - Gradle:在范围内定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788573/