Pull Gradle dependencies from CloudRepo
Gradle reads CloudRepo as a Maven repository, using the same URL and a repository token. This page covers pulling dependencies from a repository. To publish artifacts, see Publish Gradle artifacts. For an Android project, which declares its repositories in settings.gradle.kts, see Use CloudRepo with an Android project.
You need a Maven repository and a repository token that reaches it. If you have neither yet: create a repository and create a token. A token that has Download packages access is enough to pull.
Point Gradle at the repository
Section titled “Point Gradle at the repository”-
Keep the credential in
~/.gradle/gradle.properties, outside your project, so it is never committed. The username is the email address of the account that created the token, and the password is the token.~/.gradle/gradle.properties cloudrepoUsername=you@example.comcloudrepoToken=YOUR_REPOSITORY_TOKEN -
Declare the repository and a dependency in your build script, reading the credential from those two properties.
build.gradle.kts plugins {`java-library`}repositories {maven {url = uri("https://your-org.mycloudrepo.io/repositories/your-repo")credentials {username = providers.gradleProperty("cloudrepoUsername").get()password = providers.gradleProperty("cloudrepoToken").get()}}mavenCentral()}dependencies {implementation("com.example:my-library:1.0.0")}The repository URL is
https://<organization>.mycloudrepo.io/repositories/<repository>: your organization’s name, then the repository’s name as the admin portal shows it. A Groovy build script takes the same block:build.gradle repositories {maven {url = "https://your-org.mycloudrepo.io/repositories/your-repo"credentials {username = findProperty("cloudrepoUsername")password = findProperty("cloudrepoToken")}}mavenCentral()}dependencies {implementation "com.example:my-library:1.0.0"} -
Build.
Terminal gradle buildExpected:
BUILD SUCCESSFUL, withmy-libraryresolved from your CloudRepo repository.
Keep the credential in your CI’s secret store, and hand it to Gradle through the environment. Gradle
turns an environment variable named ORG_GRADLE_PROJECT_ plus a property name into that project
property, so no file is written:
export ORG_GRADLE_PROJECT_cloudrepoUsername="you@example.com"export ORG_GRADLE_PROJECT_cloudrepoToken="$CLOUDREPO_TOKEN"gradle buildKeep your own groups off other repositories
Section titled “Keep your own groups off other repositories”A build often reads CloudRepo for its own dependencies and Maven Central for the rest, as the build script above does. When a group and name is held in both places, which copy Gradle takes depends on how you write the version:
- A fixed version, such as
1.0.0. Gradle takes it from the first repository in yourrepositoriesblock that holds it. - A range such as
[1.0,), orlatest.release. Gradle reads the versions from every repository that holds the dependency and takes the highest. If Maven Central has a higher version of that name than CloudRepo does, Gradle takes Central’s, whatever order you list the repositories in.
To keep your own groups coming from CloudRepo, pick one:
Read one repository. Make a group
of your Hosted repository and a Proxy repository of Maven Central. Then,
in the repositories block under “Point Gradle at the repository”, declare the group’s URL as the one maven
repository and drop mavenCentral(). Gradle asks one address for every dependency. For a group and name your
Hosted repository holds, the group serves your Hosted repository’s files, its maven-metadata.xml included, so a
range sees your versions and not Central’s.
Pull through a group
says when that holds.
Or tell Gradle which repository holds your group. If a group of yours must come only from CloudRepo, declare that this repository holds it exclusively:
repositories { exclusiveContent { forRepository { maven { url = uri("https://your-org.mycloudrepo.io/repositories/your-repo") credentials { username = providers.gradleProperty("cloudrepoUsername").get() password = providers.gradleProperty("cloudrepoToken").get() } } } filter { includeGroup("com.example") } } mavenCentral()}Gradle then looks for artifacts of com.example only in that repository, and never asks it for any
other group. See Gradle’s documentation on exclusive content.
When Gradle answers 401, 403 or 404
Section titled “When Gradle answers 401, 403 or 404”Gradle reports the status it received. Check these in order:
- 401 Unauthorized. The credential was refused. The username must be the email address of the account that created the token, and the password the token itself. A token that is expired or revoked fails the same way: the Repository Tokens page in the admin portal shows its status. If you’ve set up a second factor, your portal password no longer works for builds. Use a repository token instead. In the Kotlin form above, a property that Gradle cannot find fails before any request, with Gradle’s own message.
- 403 Forbidden. The token does not have access to this repository. A token can only access the repositories selected when it was created, so use one that includes this repository.
- 404 Not Found. The path is wrong: check the organization and repository names in the URL, and
the group, artifact and version of the dependency. A proxy repository that the token does not reach
also answers
404, not403.
More: Repository tokens, for every client’s credential; Proxy repositories, to pull Maven Central or another public repository through CloudRepo; Use CloudRepo with an Android project, to resolve dependencies in an Android project.