如何在 Groovy 文件中使用 gradle.properties 中的值
How to use values in gradle.properties in Groovy file
我提供了一个简化的工作流程示例,并确认了新项目中所提供代码的行为:
在我的 gradle.properties
我有一行:
testFilesPath=buildSrc/src/main/resources/testFiles
它引用了指定路径中的一个文件夹,其中有 2 个文件,testFile1.txt
和 testFile2.txt
下面
我想在位于 buildSrc/src/main/groovy/TestDirectoryReader.groovy
的 Groovy class 中使用 gradle 属性,如下所示:
class TestDirectoryReader {
void printDirectoryContents() {
def testFiles = new File(System.getProperty('testFilesPath'))
testFiles.eachFile() { testFile ->
println(testFile.name)
}
}
}
此 class 文件未编译 - 我遇到错误:
> Ambiguous method overloading for method java.io.File#<init>.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.String]
[class java.net.URI]
在调试期间,我尝试专门评估 System.getProperty('testFilesPath')
并将其 returns 设为 null,这可以解释错误。所以这似乎不是从 gradle.properties.
检索值的方法
我找到了 this Whosebug post,但它似乎不太符合我的用例,我不确定我是否理解为什么我需要创建一个 JavaExec 任务来专门从 gradle.properties 我认为它应该随处可见
有人可以给我提供一个示例,说明我如何在 Groovy class 中访问 gradle.properties 中的值吗?
将我的 gradle.properties
行更改为
systemProp.testFiles=buildSrc/src/main/resources/testFiles
我在 Groovy class 中使用
调用它
def testFiles = new File(System.getProperty('testFiles'))
谢谢@daggett!
我提供了一个简化的工作流程示例,并确认了新项目中所提供代码的行为:
在我的 gradle.properties
我有一行:
testFilesPath=buildSrc/src/main/resources/testFiles
它引用了指定路径中的一个文件夹,其中有 2 个文件,testFile1.txt
和 testFile2.txt
下面
我想在位于 buildSrc/src/main/groovy/TestDirectoryReader.groovy
的 Groovy class 中使用 gradle 属性,如下所示:
class TestDirectoryReader {
void printDirectoryContents() {
def testFiles = new File(System.getProperty('testFilesPath'))
testFiles.eachFile() { testFile ->
println(testFile.name)
}
}
}
此 class 文件未编译 - 我遇到错误:
> Ambiguous method overloading for method java.io.File#<init>.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.String]
[class java.net.URI]
在调试期间,我尝试专门评估 System.getProperty('testFilesPath')
并将其 returns 设为 null,这可以解释错误。所以这似乎不是从 gradle.properties.
我找到了 this Whosebug post,但它似乎不太符合我的用例,我不确定我是否理解为什么我需要创建一个 JavaExec 任务来专门从 gradle.properties 我认为它应该随处可见
有人可以给我提供一个示例,说明我如何在 Groovy class 中访问 gradle.properties 中的值吗?
将我的 gradle.properties
行更改为
systemProp.testFiles=buildSrc/src/main/resources/testFiles
我在 Groovy class 中使用
调用它def testFiles = new File(System.getProperty('testFiles'))
谢谢@daggett!