Bilal Shah
Bilal Shah

Reputation: 1265

Gradle - Load properties from a .properties file

I am getting this error while trying to add db.properties file in build.gradle file

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

}

db.properties:

db=blal
dbUsername=/bilal/home

Error I am getting is:

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

* What went wrong:
A problem occurred evaluating script.
> Could not find property 'blal' on root project 'CamelExtractionServices'.

Upvotes: 5

Views: 14532

Answers (2)

gavenkoa
gavenkoa

Reputation: 48903

The better way is to utilize withInputStream which automatically closees a stream:

def props = new Properties()
File propsFile = file("local.properties")
if (propsFile.isFile()) {
    propsFile.withInputStream { props.load(it) }
} else {
    props.put("key", "defaultValue");
}

Upvotes: 0

RaGe
RaGe

Reputation: 23795

If you're looking to load properties from an .properties file, I would try something like this:

ext.additionalProperties = new Properties().load(file("db.properties").newReader())
ext.someOtherProperties = new Properties().load(file("foo.properties").newReader())

Then you can access your properties:

println additionalProperties['db']
println someOtherProperties['bar']

Upvotes: 4

Related Questions