尝试了解 gradle 项目属性
显然我不明白这里发生了什么。
我猜 prop2 和 prop3 无法访问,因为它们是变量而不是“项目属性”。
出现这个问题是因为我希望变量 prop2 和 prop3 在“doTheThing()”方法中可见,但我不想将它们传递进去。我希望任务、方法可以全局访问这些变量和类(但仅限于构建脚本本身内部) - 我希望将它们键入(这就是为什么 prop1 的定义不可接受)。
不过,实际上 - 我想我需要的是一些帮助来理解 Gradle 项目属性是什么以及语法 'prop1 = "blah"' 实际上在做什么。
我已经阅读了 Gradle 用户指南以及 Gradle in Action - 如果他们已经解释了这个概念,请指出我正确的部分(也许我当时忽略了它,不明白所说的内容)。
prop1 = "blah"
String prop2 = "bleah"
def prop3 = "blargh"
task testPropAccess << {
println "1: $prop1"
println "2: $prop2"
println "3: $prop3"
doTheThing()
}
private void doTheThing(){
println "4: $prop1"
println "5: $prop2" // error: Could not find property 'prop2' on root project 'script'
println "6: $prop3" // error: Could not find property 'prop3' on root project 'script'
}
Clearly I don't understand what's going on here.
I guess prop2 and prop3 can't be accessed because they are variables instead of "project properties".
The question arose because I would like the variables prop2 and prop3 to be visible from within the "doTheThing()" method, but I don't want to have to pass them in. I want the variables to be globally accessible to tasks, methods and classes (but only from within in the build script itself) - and I want them to be typed (which is why the defintion of prop1 is not acceptable).
Really, though - I guess what I'm asking for is some help understanding what a Gradle project property is and what the syntax 'prop1 = "blah"' is actually doing.
I have read the Gradle user guide and also Gradle in Action - if they already explain this concept please point me to the right section (maybe I glossed over it at the time not understanding what is was saying).
prop1 = "blah"
String prop2 = "bleah"
def prop3 = "blargh"
task testPropAccess << {
println "1: $prop1"
println "2: $prop2"
println "3: $prop3"
doTheThing()
}
private void doTheThing(){
println "4: $prop1"
println "5: $prop2" // error: Could not find property 'prop2' on root project 'script'
println "6: $prop3" // error: Could not find property 'prop3' on root project 'script'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在最外层声明变量时(如第二个和第三个语句),它就成为脚本的
run
方法的局部变量。这实际上只是 Groovy 行为,Gradle 无法轻易更改任何内容。如果您想要全局变量的等效值,只需为未绑定变量分配一个值(如第一个语句中所示)。这会向 Gradle 的
Project
对象添加一个动态属性,该属性在整个构建脚本中都是可见的(除非有阴影)。换句话说,prop1 = "blah"
相当于project.prop1 = "blah"
。如果您想要类型化全局变量的等效项,则必须等到 Gradle 升级到 Groovy 1.8,这使得通过
@Field
注释成为可能。或者您编写一个插件,将约定对象混合到Project
对象中(但这不适合临时脚本)。When you declare a variable at the outermost level (as in your second and third statement), it becomes a local variable of the script's
run
method. This is really just Groovy behavior, and nothing that Gradle can easily change.If you want the equivalent of a global variable, just assign a value to an unbound variable (as in your first statement). This adds a dynamic property to Gradle's
Project
object, which is visible throughout the build script (unless shadowed). In other words,prop1 = "blah"
is equivalent toproject.prop1 = "blah"
.If you want the equivalent of a typed global variable, you'll have to wait until Gradle upgrades to Groovy 1.8, which makes this possible with the
@Field
annotation. Or you write a plugin that mixes a convention object into theProject
object (but that's not suitable for ad-hoc scripting).