Gradle Repositories: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:
=Overview=
=Overview=


A repository is an external source of [[Gradle_Concepts#Dependency|dependencies]], a destination for the artifacts of a project, or both. More formally, a repository is a hosting location for a set of [[Gradle_Concepts#Module|modules]], each of which may provide releases identified by the [[Gradle_Concepts#Module_Version|module version]]. The repository can be based on a product such as [[Sonatype Nexus 3|Nexus]] or [[Artifactory]], or it can be a directory on a filesystem. The repositories for a project are [[Build.gradle#Repositories|declared in build.gradle]].
A repository is an external source of [[Gradle_Concepts#Dependency|dependencies]], a destination for the artifacts of a project, or both. More formally, a repository is a hosting location for a set of [[Gradle_Concepts#Module|modules]], each of which may provide releases identified by the [[Gradle_Concepts#Module_Version|module version]]. The repository can be based on a product such as [[Sonatype Nexus 3|Nexus]] or [[Artifactory]], or it can be a directory on a filesystem. Repositories are usually declared in build.gradle's [[Gradle_Project_and_Build_Script#repositories.7B.7D|repositories{...}]] script block.


==Repository Types==
==Repository Types==

Revision as of 18:45, 19 May 2018

Internal

Overview

A repository is an external source of dependencies, a destination for the artifacts of a project, or both. More formally, a repository is a hosting location for a set of modules, each of which may provide releases identified by the module version. The repository can be based on a product such as Nexus or Artifactory, or it can be a directory on a filesystem. Repositories are usually declared in build.gradle's repositories{...} script block.

Repository Types

Gradle can resolve dependencies repositories based on Maven, Ivy or flat directory formats. More: https://docs.gradle.org/current/userguide/repository_types.html.

flatDir

Custom Maven Repository

repositories {
    maven {
        // Look for POMs and artifacts here:
        url "http://example.com/maven2"
        // Optionally, if not found at the above location, look for artifacts here:
        artifactUrls "http://example.com/location"
        artifactUrls "http://example.com/location2"
        credentials {
            username "someuser"
            password "somepassword"
        }
    }
}

Pre-Defined Repositories

Some well known repositories are represented in the DSL by org.gradle.api.artifacts.dsl.RepositoryHandler methods.

Maven Central

repositories {
    mavenCentral()
}

Local Maven repository

Using a local Maven repository makes sense for teams that publish to the local Maven repository with one project and consume the artifacts by Gradle in another project. Note that Gradle has its own dependency cache. A build does not need to declare the local Maven repository even if you resolve dependencies from a Maven remote repository. Gradle uses the same logic as Maven to identify the location of the local Maven repository.

repositories {
    mavenLocal()
}

Repository Authentication

https://docs.gradle.org/current/userguide/repository_types.html#sub:authentication_schemes
repositories {
    maven {
        url 'https://repo.mycompany.com/maven2'
        credentials {
            username "someuser"
            password "somepassword"
        }
    }
}

Repository Preemptive Authentication

https://docs.gradle.org/current/userguide/repository_types.html#sub:preemptive_authentication
repositories {
    maven {
        url 'https://example.com/maven2'
        credentials {
            username "someuser"
            password "somepassword"
        }
        authentication {
            basic(BasicAuthentication)
        }
    }
}

Credential Externalization

Use properties instead of credentials in clear in build.gradle, and then initialize those properties into a local file, such as ~/.gradle/gradle.properties.

build.gradle:

repositories {
    maven {
        url 'https://example.com/maven2'
        credentials {
            username mavenUser
            password mavenPassword
        }
    }
}

~/.gradle/gradle.properties:

mavenUser=someuser
mavenPassword=somepassword