Gradle - 获取属性的默认值(约定)

发布于 2025-01-11 17:55:11 字数 798 浏览 3 评论 0原文

我想实现一个非常基本的事情(用任何普通语言):我想获得属性的默认值(约定)。 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 技术交流群。

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

发布评论

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

评论(2

自此以后,行同陌路 2025-01-18 17:55:11

答案(2022 年)是:不,您无法获取属性的默认值(约定)。

The answer (for 2022) is: No, you can't get the default value (convention) of a Property.

后来的我们 2025-01-18 17:55:11

我将尝试回答为什么 set 在调用后会覆盖约定约定是默认值,一旦通过调用set为属性赋值,约定就会被忽略,因为属性已经赋值。这是有道理的,因为这就是默认值的含义。如果我有一个值,则使用该默认值。 Conventionset 似乎遵循这种模式。我不太明白为什么这对于你想要如何使用 gradle 来说是令人惊讶的,但作为一个外部观察者,我认为 gradle 正在做预期的事情。

您可以使用以下方法测试属性是否存在:

if( property.isPresent() ) {
    // do whatever
}

我敢打赌,当 get() 返回约定时,isPresent() 会返回 false,但是如果您调用 setisPresent() 将返回 true

然后还有其他方法可以提供帮助,例如:

def val = property.getOrElse( "SomeDefault" )
def maybe = property.getOrNull()

我确实认为 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 the convention once it's called. The convention is the default value, once the property has a value by calling set the convention 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 and set 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

if( property.isPresent() ) {
    // do whatever
}

I bet that isPresent() is going to say false when the convention would be returned by get(), but if you call set then isPresent() is going to return true.

Then there are other methods to help like:

def val = property.getOrElse( "SomeDefault" )
def maybe = property.getOrNull()

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

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