HomeJavaHow to declare Gradle Repositories

How to declare Gradle Repositories

When you are developing projects using Gradle and you are not sure how to implement external dependencies to your project, then this journal entry can help you with that. Lets take some examples on understanding how to declare Gradle repositories for external dependencies. But before we start, we have to understand that the project can have multiple repositories. Gradle will look for a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.

Declare Gradle Repositories

Gradle find the external dependencies files by looking into in aĀ repositoryĀ blockĀ defined in theĀ build.gradle file. A repository is basically a collection of files, organized byĀ group,Ā name,Ā andĀ version. Gradle understands several different repository formats, such as Maven and Ivy. It also has several different ways of accessing the repository, such as using the local file system or HTTP.

By default, Gradle does not define any repositories of its own. You have to define at least one before accessing any external dependencies. One of the most commonĀ options is to use the Maven central repository:

build.gradle


repositories {
    mavenCentral()
}

Or we can use JCenter:

build.gradle


repositories {
    jcenter()
}

Gradle also gives you the option to use your own Maven or Ivy repositories server.

build.gradle


// Maven Repositories Server
repositories {
    maven {
        url "http://repo.yourcompany.com/maven2"
    }
}


// IVY Repositories Directory
repositories {
    ivy {
        url "http://repo.yourcompany.com/repo"
    }
}

You can also have repositories on the local file system. This works for both Maven and Ivy repositories.

build.gradle


repositories {
    ivy {
        // URL can refer to a local directory
        url "../local-repo-folder"
    }
}

Sometimes a repository will have the POMs published to one location, and the JARs and other artifacts published at another location. To define such a repository, you can do:

build.gradle


repositories {
    maven {

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

    }
}

 

To access the protected maven repository servers, you need to have basic authentication and have to specify the username and password to be used when you define the repository:

build.gradle


repositories {
    maven {
        credentials {
            username 'user'
            password 'password'
        }
        url "http://repo.yourcompany.com/maven2"
    }
}

 

If you like this journal entry and want some more entries like these then please stay tuned and connected byĀ subscribing to our newsletterĀ or you can join us on our social platforms ā€“Ā facebook,Ā twitter,Ā google+.

 

RELATED ARTICLES

Most Popular