Gradle Repositories: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(14 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Internal=
=Internal=


* [[Gradle_Concepts#Repository|Gradle Concepts]]
* [[Gradle_Concepts#Gradle_Repositories|Gradle Concepts]]
* [[Gradle_Project_and_Build_Script#Overview|Gradle Project and Build Script]]
 
=TODO=
<center><font color=darkgray>'''TO REFACTOR'''</font></center>


=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. Repositories for a project are usually declared and configured in the [[Gradle_Project_and_Build_Script#repositories.7B.7D|repositories{...}]] script block of the build.gradle associated with the project. At [[Gradle_Concepts#Configuration|configuration phase]], the [https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:repositories(groovy.lang.Closure) repositories{...}] script block passes the configuration closure specified in the file to its delegate object, which is a [https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html RepositoryHandler] instance.
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 to pull dependencies from for a project are declared and configured in the [[Gradle_Project_and_Build_Script#repositories.7B.7D|repositories{...}]] script block of the build.gradle associated with the project. At [[Gradle_Concepts#Configuration|configuration phase]], the [https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:repositories(groovy.lang.Closure) repositories{...}] script block passes the configuration closure specified in the file to its delegate object, which is a [https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html RepositoryHandler] instance. Note that a parent's repositories are not automatically made available to the children project in a multi-project build. A repository must be specifically configured on the project or sub-project is we need the project to access artifacts from that repository.


Note that a parent's repositories are not automatically made available to the children project in a multi-project build. A repository must be specifically configured on the project or sub-project is we need the project to access artifacts from that repository.
Repositories to publish artifacts into are specified differently, depending on the publishing plugin, though the syntax of the repository configuration closure is similar to the one described below. For more details see [[Gradle_Artifact_Publishing_Concepts#Overview|Gradle Artifact Publishing]].


=Repository Handler=
=Repository Handler=
Line 18: Line 24:


All repositories associated with a project are maintained within a '''repository handler''', which allows repositories to be defined and queried. The repository handler is one of the [[Gradle_Project_and_Build_Script#Project.27s_Containers_and_Handlers|project's containers]].
All repositories associated with a project are maintained within a '''repository handler''', which allows repositories to be defined and queried. The repository handler is one of the [[Gradle_Project_and_Build_Script#Project.27s_Containers_and_Handlers|project's containers]].
A different instantiation of the Gradle RepositoryHandler component is used by the [[Gradle_Maven_Publish_Plugin#Overview|maven-publish]] plugin, but the two RepositoryHandler instances are completely separated and serve different purposes. For more details on maven-publish plugin repositories, see: {{Internal|Gradle_Maven_Publish_Plugin#Repositories|maven-publish Plugin Repositories}}


=Repository Types=
=Repository Types=
Line 45: Line 53:
=Declaring Repositories=
=Declaring Repositories=


=Pre-Defined Repositories=
Repositories are declared in a [[Gradle_Project_and_Build_Script#repositories.7B.7D|repositories{...}]] script block. Multiple repositories can be declared in the same block.
 
==Pre-Defined Repositories==


Some well known repositories are represented in the DSL by [https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html RepositoryHandler] methods.  
Some well known repositories are represented in the DSL by [https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html RepositoryHandler] methods.  


==Maven Central==
===Maven Central===


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
Line 57: Line 67:
</syntaxhighlight>
</syntaxhighlight>


==Local Maven repository==
===<span id='Local_Maven_repository'></span>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 [[#The_Local_Gradle_Dependency_Cache|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 [[Maven_Repositories#The_Local_Repository|the same logic as Maven]] to identify the location of the 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 [[Gradle_Dependencies_and_Dependency_Configurations#The_Dependency_Cache|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 [[Maven_Repositories#The_Local_Repository|the same logic as Maven]] to identify the location of the local Maven repository.


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
Line 67: Line 77:
</syntaxhighlight>
</syntaxhighlight>


=Repository Authentication=
==Repository Authentication==


{{External|https://docs.gradle.org/current/userguide/repository_types.html#sub:authentication_schemes}}
{{External|https://docs.gradle.org/current/userguide/repository_types.html#sub:authentication_schemes}}
Line 83: Line 93:
</syntaxhighlight>
</syntaxhighlight>


==Repository Preemptive Authentication==
===Repository Preemptive Authentication===


{{External|https://docs.gradle.org/current/userguide/repository_types.html#sub:preemptive_authentication}}
{{External|https://docs.gradle.org/current/userguide/repository_types.html#sub:preemptive_authentication}}
Line 102: Line 112:
</syntaxhighlight>
</syntaxhighlight>


==Credential Externalization==
===Credential Externalization===


Use properties instead of credentials in clear in [[build.gradle#Overview|build.gradle]], and then initialize those properties into a local file, such as ~/.gradle/gradle.properties.
Use properties instead of credentials in clear in [[Gradle_Project_and_Build_Script#Overview|build.gradle]], and then initialize those properties into a local file, such as ~/.gradle/gradle.properties.


build.gradle:
build.gradle:

Latest revision as of 19:26, 23 April 2021

External

Internal

TODO

TO REFACTOR

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 to pull dependencies from for a project are declared and configured in the repositories{...} script block of the build.gradle associated with the project. At configuration phase, the repositories{...} script block passes the configuration closure specified in the file to its delegate object, which is a RepositoryHandler instance. Note that a parent's repositories are not automatically made available to the children project in a multi-project build. A repository must be specifically configured on the project or sub-project is we need the project to access artifacts from that repository.

Repositories to publish artifacts into are specified differently, depending on the publishing plugin, though the syntax of the repository configuration closure is similar to the one described below. For more details see Gradle Artifact Publishing.

Repository Handler

https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.RepositoryHandler.html

All repositories associated with a project are maintained within a repository handler, which allows repositories to be defined and queried. The repository handler is one of the project's containers.

A different instantiation of the Gradle RepositoryHandler component is used by the maven-publish plugin, but the two RepositoryHandler instances are completely separated and serve different purposes. For more details on maven-publish plugin repositories, see:

maven-publish Plugin Repositories

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"
        }
    }
}

Declaring Repositories

Repositories are declared in a repositories{...} script block. Multiple repositories can be declared in the same block.

Pre-Defined Repositories

Some well known repositories are represented in the DSL by 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