Pull Maven artifacts from CloudRepo
Maven reads CloudRepo like any other Maven repository: declare it in your pom.xml, give Maven the
credential in settings.xml, and build. To put artifacts there, see
Publish Maven artifacts.
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 Maven at the repository
Section titled “Point Maven at the repository”-
Put the credential in
~/.m2/settings.xml. The<id>is a name you choose, and the repository in yourpom.xmlmust use the same one. The username is the email address of the account that created the token, and the password is the token.~/.m2/settings.xml <settings><servers><server><id>cloudrepo</id><username>you@example.com</username><password>${env.CLOUDREPO_TOKEN}</password></server></servers></settings>${env.CLOUDREPO_TOKEN}reads the token from an environment variable, so the file holds no token. Export it in the shell that runs Maven, or in your CI’s secret store. -
Declare the repository and the dependency in
pom.xml.pom.xml <project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-app</artifactId><version>1.0.0</version><repositories><repository><id>cloudrepo</id><url>https://your-org.mycloudrepo.io/repositories/your-repo</url></repository></repositories><dependencies><dependency><groupId>com.example</groupId><artifactId>my-library</artifactId><version>1.0.0</version></dependency></dependencies></project>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. Put the<repositories>block in a parent POM if you have one, so a project states it once. -
Build.
Terminal mvn --batch-mode packageExpected: Maven prints
Downloading from cloudrepo:and the artifact’s URL under your repository, thenBUILD SUCCESS.
Fetch one file with curl
Section titled “Fetch one file with curl”When you want a single file and no build, curl sends the same credential. Put it in ~/.netrc and ask
curl to read it, and keep the file readable by you alone (chmod 600 ~/.netrc).
machine your-org.mycloudrepo.iologin you@example.compassword YOUR_REPOSITORY_TOKENcurl --netrc --fail --remote-name \ https://your-org.mycloudrepo.io/repositories/your-repo/com/example/my-library/1.0.0/my-library-1.0.0.jarThe path is your artifact’s group (dots become slashes), its name, its version and the file.
Pull through a group (Nexus maven-public, Artifactory virtual)
Section titled “Pull through a group (Nexus maven-public, Artifactory virtual)”A group is one URL in front of several Maven repositories, its members. It is CloudRepo’s answer to a
Nexus group such as maven-public and to an Artifactory virtual repository.
Create the group. In the admin portal, create a repository with Maven as the Format and Group as the Kind. Open its settings and choose Manage Group Members. Turn on Is Member for each repository the group should serve, and give each a Priority. The Management API reference lists the same operations for a script.
Which member serves a file. The lowest Priority number comes first. Members with the same number go in name order, and a member with no number goes last. The group asks its Hosted members first, in that order, and the first that holds the file serves it. A Proxy member serves a file only when no Hosted member you can read holds it. A public registry’s copy never replaces a file a Hosted member holds.
Tick the group on the token. When you create the repository token, tick the group. Through the group’s URL, the token reads the group’s current members, read only. A member’s own URL needs that member ticked too, so tick a member to use its address, such as to publish to it.
Point Maven at the group. In ~/.m2/settings.xml, a <mirror> whose <mirrorOf> is * sends every
request Maven makes to the group, Maven Central’s included. The <server> with the same <id> gives
Maven the credential:
<settings> <servers> <server> <id>cloudrepo</id> <username>you@example.com</username> <password>${env.CLOUDREPO_TOKEN}</password> </server> </servers> <mirrors> <mirror> <id>cloudrepo</id> <mirrorOf>*</mirrorOf> <url>https://your-org.mycloudrepo.io/repositories/maven-public</url> </mirror> </mirrors></settings>Put your group’s name in place of maven-public. Maven now reads every dependency through the group, so
add a Proxy repository of Maven Central to it as a member, as a
Nexus maven-public holds maven-central. Without one, a dependency from Maven Central is not found.
A group is read-only: deploy to a member. A group stores nothing of its own, so mvn deploy to a
group’s URL is refused with 405 Method Not Allowed for a token that can write, and with 403 Forbidden
for a read-only token. Name a Hosted member in your <distributionManagement>, as
Publish Maven artifacts shows. If you are moving from an Artifactory virtual
repository with a Default Deployment Repository, deploy to the hosted member and read through the group,
as the terms page maps.
What the group serves as maven-metadata.xml. Maven reads an artifact’s versions from this file, so it
decides what a version range such as [1.0,), LATEST and RELEASE can see.
| When | The group serves |
|---|---|
| Every member is Hosted, and two or more hold versions of the artifact | One merged file that lists every version they hold, in Maven’s order, with the highest release as <latest> and <release>. Its .md5, .sha1, .sha256 and .sha512 are computed from the merged file. Its .asc answers 404 Not Found. |
| One member holds versions of the artifact | That member’s file and its checksum and signature files, unchanged. |
The group has a Proxy member, the file is a -SNAPSHOT version’s, or a member holds only -SNAPSHOT versions |
The files of the first Hosted member that holds them, unchanged, so a range and LATEST see only that member’s versions. A Proxy member’s file only when no Hosted member you can read holds the artifact. |
In the last case, keep every version of an artifact in one member.
Use CloudRepo beside Maven Central
Section titled “Use CloudRepo beside Maven Central”A build often reads CloudRepo for its own artifacts and Maven Central for the rest. Maven adds Maven Central to
the repositories in your pom.xml on its own, so a build that reads CloudRepo usually reads Central too.
When a group and artifact name is held in both places, which copy Maven takes depends on how you write the version:
- A range, such as
[1.0,). Maven reads the list of versions from every repository that holds the artifact and takes the highest. If Maven Central has a higher version of that name than CloudRepo does, Maven takes Central’s. The order of your repositories does not change this. See Maven’s version requirement syntax. LATESTorRELEASE. Maven reads the metadata of every repository that holds the artifact and takes the answer from the one whose metadata was updated most recently. A public repository updated after yours can win.- An exact version, such as
1.0.0. Maven asks the repositories in the order yourpom.xmllists them and takes the first that holds that version.
To keep your own artifacts coming from CloudRepo, pick one:
Read one repository. Make a group of your
Hosted repository and a Proxy repository of Maven Central, and point
Maven at it with the <mirrorOf>*</mirrorOf> mirror shown above. Maven then asks one address for every
artifact. For an artifact your Hosted repository holds, the group serves your Hosted repository’s files, its
maven-metadata.xml included, so a range, LATEST and RELEASE see your Hosted repository’s answer and not
Central’s. The table above says when that holds.
Or use exact versions. Write the version out for your own artifacts, and list your CloudRepo repository
before any other in pom.xml, as Point Maven at the repository shows. Maven
then takes that version from CloudRepo first. Use the group when you need a range, LATEST or RELEASE.
When Maven answers 401, 403 or 404
Section titled “When Maven answers 401, 403 or 404”Maven usually prints only the status.
| Status | What it means | Fix |
|---|---|---|
401 Unauthorized |
The credential was refused. A token that is expired or revoked fails the same way. | Use the email address of the account that created the token as the username, and the token as the password. The Repository Tokens page in the admin portal shows the token’s status. If you’ve set up a second factor, your portal password no longer works for builds. Use a repository token instead. |
403 Forbidden |
The token does not reach this repository. A token reaches only the repositories ticked when it was created. | Use a token that ticks this repository. |
404 Not Found |
Nothing is at that path, or the token does not reach the Proxy repository that holds it. A Proxy repository the token does not reach answers 404, not 403. |
Check the organization and repository names in the URL, and the group, artifact and version in the dependency. Use a token that ticks the repository. Through a group’s URL, ticking the group is enough. |
More: Repository tokens, for every client’s credential; Proxy repositories, to pull Maven Central or another public repository through CloudRepo; Publish Maven artifacts, for publishing, snapshots and Overwrite Protection.