Matiss
Matiss

Reputation: 341

gradle.properties values inside buildscript

I am trying to include a classpath dependency in Gradle build script for a Java project. I have gradle.properties file which lists two variables: nexusUsername and nexusPassword. Both of these are used in project root repositories in the same manner as for buildscript repositories bellow, and it's working fine - dependencies are loaded.

However, when configuring the buildscript like this:

buildscript {
    repositories {
        maven {
            credentials {
                username nexusUsername
                password nexusPassword
            }
            url 'https://edited'
        }
    }
    dependencies {
        classpath 'edited'
    }
}

I get an error like this:

Could not GET 'https://edited.pom'. Received status code 403 from server: Forbidden

Accessing the URL given from browser with authentication works.

As far as I was able to figure out so far, buildscript gets evaluated at the very beginning of build, hence, properties might not be loaded yet? If that's true, how to load them?

Upvotes: 2

Views: 2766

Answers (2)

mud1e
mud1e

Reputation: 109

Workaround If you want to use alternative properties file, like local.properties, gradle-local.properties:

1º) In Project rootdir, add new file "add-custom-properties.gradle":

Properties props = new Properties()
props.load(new FileInputStream("$rootDir/gradle-local.properties"))
props.each { prop ->
    ext.set(prop.key, prop.value)
}

You can add logic to this previous script if you want.

2º) Edit/create your gradle-local.properties

repoUrl=https://a-url...
repoUser=a-user
repoPass=a-pass

3º) In build.gradle, settings.xml,... ( inside buildscript{...} ):

buildscript {
    apply from: "$rootDir/add-custom-properties.gradle"
    repositories {
        maven {
            url = "${repoUrl}"
            credentials {
                username = "${repoUser}"
                password = "${repoPass}"
            }
        }
    }

Upvotes: 1

Matiss
Matiss

Reputation: 341

Apparently the issue was accidentally placed extra symbol at the end of the password in gradle.properties between runs. Works fine.

Upvotes: 0

Related Questions