试图了解gradle项目的属性 [英] Trying to understand gradle project properties

查看:140
本文介绍了试图了解gradle项目的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我猜prop2和prop3不能被访问,因为它们是变量而不是项目属性。



问题出现了,因为我希望变量prop2和prop3在doTheThing()方法中可见,但我不想通过他们进来。我希望这些变量可以被全局任务,方法和类访问(但只能从构建脚本本身的内部访问) - 我想让它们被键入(这就是为什么prop1的定义不可接受)。



实际上,我想我要求的是帮助理解Gradle项目属性以及语法prop1 =blah'实际上在做什么。



我已经阅读了Gradle用户指南以及Gradle in Action - 如果他们已经解释了这个概念,请将我指向正确的部分(也许我在当时掩盖了它

  prop1 =blah
String prop2 =bleah
def prop3 =blargh

任务testPropAccess<< {
println1:$ prop1
println2:$ prop2
println3:$ prop3
doTheThing()
}

private void doTheThing(){
println4:$ prop1
println5:$ prop2//错误:无法在根项目'脚本'上找到属性'prop2'
println6:$ prop3//错误:无法在根项目'脚本'上找到属性'prop3'
}


解决方案

当你在最外层声明一个变量时(就像在你的第二和第三条语句中一样),它就变成了脚本的 run 方法。这实际上只是Groovy的行为,并且Gradle无法轻易改变。

如果您想要一个全局变量的等价物,只需将一个值赋给一个未绑定的变量(就像在第一个语句中一样)。这为Gradle的 Project 对象添加了一个动态属性,该对象在整个构建脚本中都是可见的(除非有影子)。换句话说, prop1 =blah相当于 project.prop1 =blah



如果你想要一个类型化的全局变量,你必须等到Gradle升级到Groovy 1.8,这可以通过 @Field 注释。或者你编写一个插件,将一个约定对象混合到 Project 对象中(但不适合ad-hoc脚本)。


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'
}

解决方案

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).

这篇关于试图了解gradle项目的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆