尝试了解 gradle 项目属性

发布于 2024-12-09 03:29:44 字数 810 浏览 0 评论 0原文

显然我不明白这里发生了什么。

我猜 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2024-12-16 03:29:44

当您在最外层声明变量时(如第二个和第三个语句),它就成为脚本的 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 to project.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 the Project object (but that's not suitable for ad-hoc scripting).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文