Gradle 2.0 - 加载Java属性文件的正确方法是什么?

3
在Gradle中,您可以通过以下方式从属性文件加载数据:
apply from: "version.properties"

从Gradle 2.0开始,这会生成一个警告:

"Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtensi‌​on.html for information on the replacement for dynamic properties."

在最新版本的Gradle中,相同行为的规范方式是什么?

当然,我可以通过定义一个辅助方法来解决这个问题,但我很好奇是否有更简洁的方法。


链接已损坏,但是所指的文档在这里 - chrylis -cautiouslyoptimistic-
谢谢你修复了这个问题。我之前也检查过,但似乎没有提到这个特定的用例。 - BitPusher
1个回答

7

除了 gradle.properties 以外,从属性文件中加载数据的正确方法一直是使用 Java 的 Properties 类:

def versionProperties = new Properties()
file("version.properties").withReader { versionProperties.load(it) }

// Perhaps a future Groovy version could simplify this to:   
def versionProperties = file("version.properties").loadProperties()

仅通过apply from: "version.properties"加载属性只是巧合,并且仅当所有属性值都为数字时才有效(因为此时属性文件语法成为Groovy语法的子集)。在1.x版中,弃用警告将在至少过去一年的1.x版本中出现。2.0版本最终删除了引入属性而不声明defext.的能力,这就解释了为什么它会彻底失败。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接