Gradle - 获取属性的默认值(约定)
我想实现一个非常基本的事情(用任何普通语言):我想获得属性的默认值(约定)。 Gradle 文档中的章节 延迟配置 描述了如何将约定应用于a 属性:
def property = objects.property(String)
// Set a convention
property.convention("convention 1")
println("value = " + property.get())
// Can replace the convention
property.convention("convention 2")
println("value = " + property.get())
property.set("value")
// Once a value is set, the convention is ignored
property.convention("ignored convention")
println("value = " + property.get())
问题是,当您设置值时,您无法查询属性来获取约定(默认值)的信息。似乎唯一的方法是清除(无效)该值:
property.value(null).get()
但这很愚蠢,因为您正在执行不必要的操作,而约定就在那里?
有谁知道如何在不清除价值的情况下获得它吗?
I want to achieve a very basic thing (in any normal language): I would like to get the default value (convention) of a Property. Gradle docs in chapter Lazy configuration, describes how to apply the convention to a Property:
def property = objects.property(String)
// Set a convention
property.convention("convention 1")
println("value = " + property.get())
// Can replace the convention
property.convention("convention 2")
println("value = " + property.get())
property.set("value")
// Once a value is set, the convention is ignored
property.convention("ignored convention")
println("value = " + property.get())
The problem is, that you cannot query the property
to get information what is the convention (the default value) when you set a value. It seems that the only way it's to clear (nullify) the value:
property.value(null).get()
But this is just stupid because you are doing unnecessary actions, while the convention is somewhere there?
Does anyone know how to get it without clearing the value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案(2022 年)是:不,您无法获取属性的默认值(约定)。
The answer (for 2022) is: No, you can't get the default value (convention) of a Property.
我将尝试回答为什么
set
在调用后会覆盖约定
。约定
是默认值,一旦通过调用set
为属性赋值,约定
就会被忽略,因为属性已经赋值。这是有道理的,因为这就是默认值的含义。如果我有一个值,则使用该默认值。Convention
和set
似乎遵循这种模式。我不太明白为什么这对于你想要如何使用 gradle 来说是令人惊讶的,但作为一个外部观察者,我认为 gradle 正在做预期的事情。您可以使用以下方法测试属性是否存在:
我敢打赌,当
get()
返回约定时,isPresent()
会返回 false,但是如果您调用set
则isPresent()
将返回true
。然后还有其他方法可以提供帮助,例如:
我确实认为 Gradle 并不总是能够很好地从其主页和文档(即 groovydoc)访问 API 来回答更复杂的问题:
https://docs.gradle.org/current/ javadoc/org/gradle/api/provider/Property.html
这些方法是超级接口 Provider,因此如果您不查找链,它们可能并不明显可用:
https://docs.gradle.org/current/javadoc/ org/gradle/api/provider/Provider.html
I'll try to answer why
set
overrides theconvention
once it's called. Theconvention
is the default value, once the property has a value by callingset
theconvention
is ignored because the property has a value. That makes sense because that's what a default value should mean. If I have a value use that otherwise use this default value.Convention
andset
seem to follow that pattern. I don't quite follow why this is surprising to how you want to use gradle, but just as an outside observer I think gradle is doing what is expected.You can test if a property exists using
I bet that
isPresent()
is going to say false when the convention would be returned byget()
, but if you callset
thenisPresent()
is going to returntrue
.Then there are other methods to help like:
I do think Gradle doesn't always do a good job of making the API very accessible from their home page and docs (ie groovydoc) for answering more complex questions:
https://docs.gradle.org/current/javadoc/org/gradle/api/provider/Property.html
These methods are the super interface Provider and hence why it may not be obvious they are available if you don't look up the chain:
https://docs.gradle.org/current/javadoc/org/gradle/api/provider/Provider.html