Publish Gradle artifacts to CloudRepo
Gradle publishes to CloudRepo through its maven-publish plugin. CloudRepo is a Maven repository, so Gradle uses the same repository URL and repository token as Maven. This page covers publishing a build. To use what you published, see Gradle and Maven repositories.
Before you start
Section titled “Before you start”- A Maven repository. If you have none, create one.
- A repository token for that repository with Download and publish packages. A Download packages token can pull but not publish; CloudRepo answers
gradle publishwith403. See Repository tokens to create one. Your username is the email address of the account that created the token, and your password is the token. - Your repository’s URL,
https://<organization>.mycloudrepo.io/repositories/<repository>: your organization’s name, then the repository’s name as the admin portal shows it.
Publish with gradle publish
Section titled “Publish with gradle publish”1. Keep the credential in ~/.gradle/gradle.properties, outside your project, so it is never
committed. The username is your email address, and the password is the token. Keep the file readable by
you alone (chmod 600 ~/.gradle/gradle.properties), because it holds the token.
cloudrepoUsername=you@example.comcloudrepoToken=YOUR_REPOSITORY_TOKEN2. Add the plugin, a publication and the repository to your build script. The maven-publish plugin
gives you the publish task. The publication says what to publish, and the repository says where. If you
start a new build, begin your build.gradle with the plugin. Its group and version name your
artifact, so put your own there:
plugins { id 'java' id 'maven-publish'}
group = 'com.example.docs'version = '1.0.0'Then add the publication and the repository below it. They read the credential from the two properties above:
publishing { publications { library(MavenPublication) { from components.java } } repositories { maven { url = "https://your-org.mycloudrepo.io/repositories/your-repo" credentials { username = findProperty("cloudrepoUsername") password = findProperty("cloudrepoToken") } } }}A Kotlin build script takes both parts in one file. Save it as build.gradle.kts:
plugins { java `maven-publish`}
group = "com.example.docs"version = "1.0.0"
publishing { publications { create<MavenPublication>("library") { from(components["java"]) } } repositories { maven { url = uri("https://your-org.mycloudrepo.io/repositories/your-repo") credentials { username = providers.gradleProperty("cloudrepoUsername").get() password = providers.gradleProperty("cloudrepoToken").get() } } }}Already have a build script? Keep it, with its own group and version, and add the publishing
block to it. Then add the plugin. In a Groovy script, add this line above the publishing block:
apply plugin: "maven-publish"Gradle reads a plugins block only before the rest of the build script, so don’t add a second one further
down. In a Kotlin script, add `maven-publish` to the plugins block at the top of the file instead.
apply(plugin = "maven-publish") is not enough there, because the script then does not know the
publishing block.
3. Publish. Run this in your project’s directory:
gradle publishExpected: BUILD SUCCESSFUL, with a publish...PublicationToMavenRepository task in the output. Gradle
uploads the JAR, the POM and a Gradle module metadata file (.module), each with its checksum files,
then maven-metadata.xml.
4. Check that it landed. Fetch the POM you published. Put the credential 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 --output docs-gradle-1.0.0.pom \ https://your-org.mycloudrepo.io/repositories/your-repo/com/example/docs/docs-gradle/1.0.0/docs-gradle-1.0.0.pomThe path under the repository URL is your project’s group (dots become slashes), the artifact name
(your project’s name), the version and the file. The artifact is also listed in the repository in the
admin portal.
Publish from CI
Section titled “Publish from CI”Keep the token in your CI’s secret store and pass it to Gradle through the environment, so no file is written. Gradle turns an environment variable named ORG_GRADLE_PROJECT_ plus a property name into that project property, so the same build script works unchanged:
export ORG_GRADLE_PROJECT_cloudrepoUsername="you@example.com"export ORG_GRADLE_PROJECT_cloudrepoToken="$CLOUDREPO_TOKEN"gradle publishSnapshots and releases
Section titled “Snapshots and releases”Give a build in progress a version that ends in -SNAPSHOT, such as version = "1.0.1-SNAPSHOT", and
CloudRepo accepts it again on every publish. A release version, one that does not end in -SNAPSHOT, is
published once: a second publish to the same version is refused with 409, and the artifact already there
stays as it is. See Overwrite Protection, the
default setting behind this.
When a publish is refused
Section titled “When a publish is refused”Gradle prints the status it received, such as
Could not PUT '...'. Received status code 409 from server: Conflict, and not the explanation
CloudRepo sends with it. 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 block, a property that Gradle cannot find fails before any request, with Gradle’s own message.
- 403 Forbidden. The token cannot publish here. A Download packages token cannot publish at all, and a token only works for the repositories selected when it was created. Use a Download and publish packages token that includes this repository.
- 409 Conflict. You published a release version that is already in the repository. Nothing is
broken: CloudRepo refuses to replace a release on purpose. Publish a new version, or use a
-SNAPSHOTversion while you iterate. If the repository is meant to accept republished releases, turn Overwrite Protection off in its settings, knowing that a build that resolved that version yesterday can then get different bytes today. - 413 Request Entity Too Large. One file is over 50 GB (50,000,000,000 bytes), the most a single file can be.
More: Repository tokens, for every client’s credential; Publish Maven artifacts, if Maven builds your project.