ali haider
ali haider

Reputation: 20242

setting property read from local.properties inside build.gradle

I am using grade v3.4. I have moved the following properties to a file called local.properties (same directory as build.gradle):

local.properties

nexusUsername=someuser
nexusPassword=somepassword

build.gradle

File secretPropsFile = file('./local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
       project.set name, value
    }
} else {
    throw new IllegalStateException("secret.properties could not be located for build process")
}

I am getting the following exception:

Could not find method set() for arguments [nexusUsername, someuser] on root project 'some-java-project of type org.gradle.api.Project.

Upvotes: 2

Views: 1593

Answers (1)

Opal
Opal

Reputation: 84854

The error you're getting is correct. To set a property you need to use ext. Please have a look at the docs.

So the following piece of code will do the job:

File secretPropsFile = file('./local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        ext[name] = value
    }
} else {
    throw new IllegalStateException("secret.properties could not be located for build process")
}

println project.nexusPassword
println project.nexusUsername //property is set in project's scope via ext

Upvotes: 2

Related Questions