# 3. Declaring repositories

version 9.2.0

Gradle needs to know where it can download the dependencies used in the project.

For example, the `com.google.guava:guava:30.0-jre` dependency can be downloaded from the public Maven Central repository `mavenCentral()`. Gradle will find and download the `guava` source code (as a `jar`) from Maven Central and use it build the project.

You can add any number of repositories for your dependencies by configuring the `repositories` block in your `build.gradle(.kts)` file:

build.gradle.kts

    repositories {
        mavenCentral()  (1)
        maven {         (2)
            url = uri("https://company/com/maven2")
        }
        mavenLocal()    (3)
        flatDir {       (4)
            dirs("libs")
        }
    }

1 | Public repository
2 | Private/Custom repository
3 | Local repository
4 | File location

build.gradle

    repositories {
        mavenCentral()  (1)
        maven {         (2)
            url = uri("https://company/com/maven2")
        }
        mavenLocal()    (3)
        flatDir {       (4)
            dirs "libs"
        }
    }

1 | Public repository
2 | Private/Custom repository
3 | Local repository
4 | File location

Gradle can resolve dependencies from one or many repositories based on Maven, Ivy or flat directory formats.

If a library is available from more than one of the listed repositories, Gradle will simply pick the first one.

## Declaring a public repository

Organizations building software may want to leverage public binary repositories to download and consume open source dependencies. Popular public repositories include Maven Central and the Google Android repository.

Gradle provides built-in shorthand notations for these widely-used repositories:

build.gradle.kts

    repositories {
        mavenCentral()
        google()
        gradlePluginPortal()
    }

build.gradle

    repositories {
        mavenCentral()
        google()
        gradlePluginPortal()
    }

Under the covers Gradle resolves dependencies from the respective URL of the public repository defined by the shorthand notation. All shorthand notations are available via the RepositoryHandler API.

## Declaring a private or custom repository

Most enterprise projects establish a binary repository accessible only within their intranet. In-house repositories allow teams to publish internal binaries, manage users and security, and ensure uptime and availability.

Specifying a custom URL is useful for declaring less popular but publicly-available repositories. Repositories with custom URLs can be specified as Maven or Ivy repositories by calling the corresponding methods available on the RepositoryHandler API:

build.gradle.kts

    repositories {
        maven {
            url = uri("https://maven-central.storage.apis.com")
        }
        ivy {
            url = uri("https://github.com/ivy-rep/")
        }
    }

build.gradle

    repositories {
        maven {
            url = uri("https://maven-central.storage.apis.com")
        }
        ivy {
            url = uri("https://github.com/ivy-rep/")
        }
    }

## Declaring a local repository

Gradle can consume dependencies available in a local Maven repository.

To declare the local Maven cache as a repository, add this to your build script:

build.gradle.kts

    repositories {
        mavenLocal()
    }

build.gradle

    repositories {
        mavenLocal()
    }

## Understanding supported repository types

Gradle supports a wide range of sources for dependencies, both in terms of format and in terms of connectivity. You may resolve dependencies from:

- Different formats

  - a Maven compatible artifact repository (e.g: Maven Central)

  - an Ivy compatible artifact repository (including custom layouts)

  - local (flat) directories

- with different connectivity

  - authenticated repositories

  - a wide variety of remote protocols such as HTTPS, SFTP, AWS S3 and Google Cloud Storage based on the presence of artifacts.

Here is a quick snapshot:

build.gradle

    repositories {

        // Ivy Repository with Custom Layout
        ivy {
            url = 'https://your.ivy.repo/url'
            layout 'pattern', {
                ivy '[organisation]/[module]/[revision]/[type]s/[artifact]-[revision].[ext]'
                artifact '[organisation]/[module]/[revision]/[type]s/[artifact]-[revision].[ext]'
            }
        }

        // Authenticated HTTPS Maven Repository
        maven {
            url = 'https://your.secure.repo/url'
            credentials {
                username = 'your-username'
                password = 'your-password'
            }
        }

        // SFTP Repository
        maven {
            url = 'sftp://your.sftp.repo/url'
            credentials {
                username = 'your-username'
                password = 'your-password'
            }
        }

        // AWS S3 Repository
        maven {
            url = "s3://your-bucket/repository-path"
            credentials(AwsCredentials) {
                accessKey = 'your-access-key'
                secretKey = 'your-secret-key'
            }
        }

        // Google Cloud Storage Repository
        maven {
            url = "gcs://your-bucket/repository-path"
        }
    }

Next Step: Learn about Centralizing Dependencies >>