user2197116
user2197116

Reputation: 677

What is the best way to read in gradle repository credentials from a properties file?

It's not clear to me where logging into a repository happens in the gradle execution/configuration chain

I have a task

loadMyProperties {
    Properties props = new Properties()
    props.load(new FileInputStream(MyPropertiesFilename))
    myusername =  props.getProperty('user')
    mypassword = props.getProperty('password')

}

and I make the compile depend on it

compileJava.dependsOn loadProperties

However, I am not at all sure when the repositories block

repositories {
    maven {
            credentials {
                    username myusername
                    password mypassword
            }
            url myurl               
    }
}

is 'executed' compared to the other tasks, nor when it attempts to gain authorization with the specified repository with the provided credentials. When I run

gradle build

Sometimes the credentials work, and sometimes they don't (I get a 401 authorization error back from the maven server), which makes me think I am not properly ordering my tasks.

My thinking was that the loadProperties code happens inside the configuration phase (since it's not in a doFirst, doLast, or <<, and thus not in the execution phase), but I'm not sure how to ensure it happens before the repository block attempts to secure authorization.

One possible workaround is to use the gradle.properties file and define myusername and mypassword in them, but want to gain an understanding of how to properly use other properties files as well and not have to rely on gradle.properties.

Upvotes: 2

Views: 1014

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

Loading a properties file is an act of configuration, and should be done outside any task.

Upvotes: 1

Related Questions