Gradle 使用自定义属性文件

Gradle 使用自定义属性文件,gradle,properties-file,Gradle,Properties File,我希望有一个与我的应用程序一起部署的属性文件,以便我可以在运行时访问它。如何让我的build.gradle文件访问位于src/main/resources中的属性文件 我希望像正常使用gradle.properties一样使用此文件。是否要在build.gradle文件中读取此属性文件?你可以简单地: def props = new Properties() file("src/main/resources/my.properties").withInputStream { strea

我希望有一个与我的应用程序一起部署的属性文件,以便我可以在运行时访问它。如何让我的
build.gradle
文件访问位于
src/main/resources
中的属性文件


我希望像正常使用
gradle.properties

一样使用此文件。是否要在build.gradle文件中读取此属性文件?你可以简单地:

def props = new Properties()
file("src/main/resources/my.properties").withInputStream { 
    stream -> props.load(stream) 
}

如果要从外部.properties文件加载属性并将其与gradle.properties中的属性合并,请使用以下方法:

def loadExtraProperties(String fileName) {
    def props = new Properties()
    props.load(new FileInputStream(fileName))

    props.each { key, val ->
        project.set(key, val)
    }
}

我希望像正常使用gradle.properties一样使用此文件。您可以迭代键/值对并将它们作为额外属性添加。是否有方法将另一个文件作为参数传递给gradle?像gradle--properties file=myfile.properties任务一样,最好使用
file().withInputStream()
,这样即使gradle守护进程仍在运行,流也会正确关闭。