Learn How To Add Multiple Repositories In Your Gradle Files To Fetch Jars

Follow below steps to have build.gradle file maps to multiple repositories.

If there is only one repo then, then its pretty stright forward.

repositories {
    maven {
        url "http://repo.mycompany.com/maven2"
    }
}

Gradle will look at the base url location for the POM and the JAR. If the JAR can’t be found there, the extra artifactUrls are used to look for JARs.

repositories {
    maven {
        // Look for POMs and artifacts, such as JARs, here
        url "http://repo2.mycompany.com/maven2"
        // Look for artifacts here if not found at the above location
        artifactUrls "http://repo.mycompany.com/jars"
        artifactUrls "http://repo.mycompany.com/jars2"
    }
}

And multiple repos can be added like below to look one after another

repositories {
  maven { url "http://maven.springframework.org/release" }
  maven { url "http://maven.restlet.org" }
  mavenCentral()
}

And if the declaration needs to be made at project level then use like below.

allprojects {
 repositories {
    jcenter()
    maven { url "http://maven.springframework.org/release" }
    maven { url "http://maven.restlet.org" }
 }
}

Tip:

If your repository is behind proxy then in gradle.properties add below properties to fetch jars without issues.

systemProp.http.proxyHost=<your_company_repo>
systemProp.http.proxyPort=<port>

References:

  1. https://docs.gradle.org/current/userguide/declaring_repositories.html

Leave a Reply

Your email address will not be published. Required fields are marked *