Eduardo
Eduardo

Reputation: 7141

Gradle - Conditionally add configuration

I have a gradle project with configuration below

apply plugin: 'java'
apply plugin: 'maven'

repositories {
    mavenCentral()

    maven {
        credentials {
            username "$System.env.REPOSITORY_USER"
            password "$System.env.REPOSITORY_PWD"
        }
        url "$System.env.REPOSITORY_HOME" + /nexus/content/groups/public/"
    }
}

Ideally only the build server should know the repository username and password that has publish rights, everyone else should have read only access (the credentials block should not be applied). Is there a way I could conditionally add the credentials block based on if both REPOSITORY_USER and REPOSITORY_PWD is populated?

I'm open to better solutions if you have any suggestions!

Upvotes: 3

Views: 1581

Answers (1)

Salem
Salem

Reputation: 12986

Try to use something like this:

repositories {
    mavenCentral()
}

if (System.env.REPOSITORY_USER != null && System.env.REPOSITORY_PWD != null) {
    repositories {
        maven {
            // Setup here what you need
            credentials {
            }
        }
    }
}

Upvotes: 5

Related Questions