Gradle - 从.properties文件加载属性

Bil*_*hah 5 gradle build.gradle

我在尝试db.propertiesbuild.gradle文件中添加文件时收到此错误

的build.gradle:

allprojects {

    apply from: 'db.properties'
    apply from: 'deployment.properties'

    repositories {
        mavenCentral()
    }

    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'maven-publish'
    apply plugin: 'idea'

    sourceCompatibility = 1.8

}
Run Code Online (Sandbox Code Playgroud)

db.properties:

db=blal
dbUsername=/bilal/home
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

* Where:
Script 'camelawsextractionservicesb/db.properties' line: 1

* What went wrong:
A problem occurred evaluating script.
> Could not find property 'blal' on root project 'CamelExtractionServices'.
Run Code Online (Sandbox Code Playgroud)

RaG*_*aGe 4

如果您想从.properties文件加载属性,我会尝试这样的操作:

ext.additionalProperties = new Properties().load(file("db.properties").newReader())
ext.someOtherProperties = new Properties().load(file("foo.properties").newReader())
Run Code Online (Sandbox Code Playgroud)

然后您可以访问您的属性:

println additionalProperties['db']
println someOtherProperties['bar']
Run Code Online (Sandbox Code Playgroud)

  • 如果 Properties 的 `load()` 方法返回 `void` ,那它应该如何工作? (6认同)
  • 如果您只需要一个“Properties”实例,您可以使用“addAll()”方法“合并”两个属性。 (2认同)