Lawrence Tierney
Lawrence Tierney

Reputation: 898

Gradle - "override" behaviour/properties from build.gradle

I'm not a Gradle expert by any means so please be gentle...

I have a Gradle build which I'm trying to run on Jenkins. The build.gradle contains the following:

repositories {
    maven {
        url "http://some_internal_corporate_repo"
    }
}

The Jenkins server that I am running the job on cannot access "some_internal_corporate_repo".

As I can't modify the build.gradle I would like to know if there's a way I can somehow extend or override the build.gradle, on the Jenkins server, to point to mavenCentral (or similar), maybe via an init file or setting a property etc?

Thanks in advance

EDIT: in the end, because I was using Jenkins, I used it's Groovy support (execute Groovy build step) to address my issue:

def file = new File('build.gradle')
def newConfig = file.text.replace('url "http://some_internal_corporate_repo"', 'url "http://repo.maven.apache.org/maven2/"')
file.text = newConfig

Upvotes: 1

Views: 1675

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363667

You can define multiple repositories.

The order of declaration determines how Gradle will check for dependencies at runtime

repositories {
    maven {
        url "http://some_internal_corporate_repo"
    }
    mavenCentral() 
}

You can use a properties to define the maven repo url:

repositories {
    maven {
        url "${repositories_maven_url}"
    }
}

In the gradle.properties file

repositories_maven_url=maven_url

According to the gradle documentation, gradle.properties files are applied in the following order:

  • gradle.properties in project root directory.
  • gradle.properties in GRADLE_USER_HOME directory.
  • system properties, e.g. when -Dgradle.user.home is set on the command line.

Or you can use something like this:

repositories {
        maven {
            url getMavenUrl()
        }
}

/**
 * Returns the url of the maven repo.
 * Set this value in your ~/.gradle/gradle.properties with repositories_maven_url key
 * If the property is not defined returns a default value
 * @return
 */
def getMavenUrl() {
    return hasProperty('repositories_maven_url') ? repositories_maven_url : "YOUR_DEFAULT_VALUE"
}

Upvotes: 1

Related Questions