Maven Artifact Registry
About 1405 wordsAbout 5 min
Prerequisites
Local Development
Configure Credentials
Copy the following content into the settings.xml file, replace <YOUR_TOKEN> with your token, and <REPO_URL> with the Artifact Registry address.
<settings>
<servers>
<server>
<id>cnb-maven-repo</id>
<username>cnb</username>
<!-- Replace with your access token -->
<password><YOUR_TOKEN></password>
</server>
</servers>
<profiles>
<profile>
<id>cnb-maven-repo-profile</id>
<repositories>
<repository>
<!-- Must match the server's id -->
<id>cnb-maven-repo</id>
<!-- Replace with the Artifact Registry address -->
<!-- Example
<url>https://maven.cnb.cool/cnb/maven-repo/-/packages/</url>
-->
<url><REPO_URL></url>
</repository>
</repositories>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</settings>- Configure the token in gradle.properties in the project root directory, replace
<YOUR_TOKEN>with your token.
cnbArtifactsGradlePassword=<YOUR_TOKEN>- Paste the following content into build.gradle.kts, replace
<REPO_URL>with the Artifact Registry address.
publishing {
repositories {
maven {
val cnbArtifactsGradlePassword = project.findProperty("cnbArtifactsGradlePassword")
// Example
// url = uri("https://maven.cnb.cool/cnb/maven-repo/-/packages/")
url = uri("<REPO_URL>")
credentials {
username = "cnb"
password = cnbArtifactsGradlePassword.toString()
}
}
}
}- Configure the token in gradle.properties in the project root directory, replace
<YOUR_TOKEN>with your token.
cnbArtifactsGradlePassword=<YOUR_TOKEN>- Paste the following content into build.gradle, replace
<REPO_URL>with the Artifact Registry address.
publishing {
repositories {
maven {
def cnbArtifactsGradlePassword = project.findProperty('cnbArtifactsGradlePassword')
// Example
// url = uri("https://maven.cnb.cool/cnb/maven-repo/-/packages/")
url = uri('<REPO_URL>')
credentials {
username = 'cnb'
password = cnbArtifactsGradlePassword.toString()
}
}
}
}Pull Artifacts
- Configure the package you need to pull in pom.xml.
<dependencies>
<dependency>
<groupId>[GROUP_ID]</groupId>
<artifactId>[ARTIFACT_ID]</artifactId>
<version>[VERSION]</version>
</dependency>
</dependencies>
<!-- Example
<dependencies>
<dependency>
<groupId>org.cnb</groupId>
<artifactId>maven_demo</artifactId>
<version>9.0.0</version>
</dependency>
</dependencies> -->- Pull artifacts.
mvn clean install
# If using a specific settings.xml, execute:
mvn clean install -s ./settings.xml- Configure the dependencies you need in build.gradle.kts.
dependencies {
implementation("[GROUP_ID]:[ARTIFACT_ID]:[VERSION]")
}
// Example
// dependencies {
// implementation("com.google.guava:guava:32.1.3-jre")
// }- Pull artifacts.
./gradlew build --refresh-dependencies- Configure the dependencies you need in build.gradle.
dependencies {
implementation '[GROUP_ID]:[ARTIFACT_ID]:[VERSION]'
}
// Example
// dependencies {
// implementation 'com.google.guava:guava:32.1.3-jre'
// }- Pull artifacts.
./gradlew build --refresh-dependenciesPush Artifacts
- Configure the publishing repository and artifact properties in pom.xml, replace
<REPO_URL>with the Artifact Registry address.
<project>
<modelVersion>4.0.0</modelVersion>
<!-- Artifact properties -->
<groupId>[GROUP_ID]</groupId>
<artifactId>[ARTIFACT_ID]</artifactId>
<version>[VERSION]</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Publishing repository -->
<distributionManagement>
<repository>
<!-- Must match the server's id in settings.xml -->
<id>cnb-maven-repo</id>
<name>cnb-maven-repo</name>
<!-- Example
<url>https://maven.cnb.cool/cnb/maven-repo/-/packages/</url>
-->
<url><REPO_URL></url>
</repository>
</distributionManagement>
</project>- Push artifacts.
mvn deploy
# If using a specific settings.xml, execute:
mvn deploy -s ./settings.xml- Paste the following content into build.gradle.kts.
plugins {
`java-library`
`maven-publish`
}
group = "[GROUP_ID]"
version = "[VERSION]"
val artifactName = "[ARTIFACT_ID]"
publishing {
publications {
create<MavenPublication>("myLibrary") {
groupId = group.toString()
version = version.toString()
artifactId = artifactName
from(components["java"])
}
}
}- Execute the upload command.
./gradlew publish- Paste the following content into build.gradle.
plugins {
id 'java-library'
id 'maven-publish'
}
group = '[GROUP_ID]'
version = '[VERSION]'
def artifactName = '[ARTIFACT_ID]'
publishing {
publications {
myLibrary(MavenPublication) {
groupId = group
version = version
artifactId = artifactName
from components.java
}
}
}- Execute the upload command.
./gradlew publishCloud Native Build
Configure Credentials
In Cloud Native Build, use the built-in environment variable CNB_TOKEN.
Other Token Methods
To use plaintext tokens, replace <YOUR_TOKEN> with your token. Or reference via a secret registry variable <ENV_NAME>. See [Using Tokens in Cloud Native Build and Development] (./intro.md#using-tokens-in-workspaces-and-cloud-native-build) for details.
Replace <REPO_URL> with the Artifact Registry address.
<settings>
<servers>
<server>
<id>cnb-maven-repo</id>
<username>cnb</username>
<password>${env.CNB_TOKEN}</password>
<!-- Alternative methods: -->
<!--<password><YOUR_TOKEN></password> Direct token -->
<!--<password>${env.<ENV_NAME>}</password> Secret registry -->
</server>
</servers>
<profiles>
<profile>
<id>cnb-maven-repo-profile</id>
<repositories>
<repository>
<!-- Must match the server's id -->
<id>cnb-maven-repo</id>
<url><REPO_URL></url>
</repository>
</repositories>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</settings>Replace <REPO_URL> with the Artifact Registry address.
publishing {
repositories {
maven {
val cnbArtifactsGradlePassword = System.getenv("CNB_TOKEN")
// Alternative methods:
// val cnbArtifactsGradlePassword = "<YOUR_TOKEN>" Direct token
// val cnbArtifactsGradlePassword = System.getenv("ENV_NAME") Secret registry
url = uri("<REPO_URL>")
credentials {
username = "cnb"
password = cnbArtifactsGradlePassword.toString()
}
}
}
}Replace <REPO_URL> with the Artifact Registry address.
publishing {
repositories {
maven {
def cnbArtifactsGradlePassword = System.getenv("CNB_TOKEN")
// Alternative methods:
// def cnbArtifactsGradlePassword = '<YOUR_TOKEN>' Direct token
// def cnbArtifactsGradlePassword = System.getenv("ENV_NAME") Secret registry
url = uri('<REPO_URL>')
credentials {
username = 'cnb'
password = cnbArtifactsGradlePassword.toString()
}
}
}
}Pull Artifacts
Paste the following content into .cnb.yml:
main:
push:
- docker:
image: maven:3.8.5-openjdk-17
stages:
- name: mvn package
script:
mvn clean install -s ./settings.xmlmain:
push:
- docker:
image: gradle:7.6.6-jdk17
stages:
- name: gradle build and run
script:
- ./gradlew build --refresh-dependenciesPush Artifacts
Paste the following content into .cnb.yml:
main:
push:
- docker:
image: maven:3.8.5-openjdk-17
stages:
- name: mvn package
script:
mvn clean deploy -s ./settings.xmlmain:
push:
- docker:
image: gradle:7.6.6-jdk17
stages:
- name: gradle publish
script:
- ./gradlew publishWorkspaces
Configure Credentials
Same as Cloud Native Build Configure Credentials
Configure Development Image
$:
vscode:
- docker:
image: maven:3.8.5-openjdk-17Pull Artifacts
Same as Local Development Pull Artifacts
Push Artifacts
Same as Local Development Push Artifacts
Examples
Gradle Local Push AAR Package
- Configure the android-library plugin in gradle/libs.versions.toml.
[versions]
agp = "8.11.1"
[plugins]
android-library = { id = "com.android.library", version.ref = "agp" }- Configure the aar packaging plugin and maven-publish plugin in build.gradle or build.gradle.kts.
// build.gradle.kts
plugins {
id("com.android.library") // AAR packaging plugin
id("maven-publish")
}// build.gradle
plugins {
id 'com.android.library' // AAR packaging plugin
id 'maven-publish'
}- Configure the build task and push address in build.gradle or build.gradle.kts.
// build.gradle.kts
afterEvaluate {
publishing {
publications {
create<MavenPublication>("releaseAar") {
groupId = "cnb"
artifactId = "artifact"
version = "1.0.0"
from(components["release"])
}
}
repositories {
maven {
// Declare cnbArtifactsGradlePassword in gradle.properties or export CNB_TOKEN=${YOUR_TOKEN}
val cnbArtifactsGradlePassword =
System.getenv("CNB_TOKEN") ?: project.findProperty("cnbArtifactsGradlePassword")
// Example
// url = uri("https://maven.cnb.cool/cnb/maven-repo/-/packages/")
url = uri("<REPO_URL>")
credentials {
username = "cnb"
password = cnbArtifactsGradlePassword.toString()
}
}
}
}
}// build.gradle
afterEvaluate {
publishing {
publications {
releaseAar(MavenPublication) {
groupId = 'cnb'
artifactId = "artifact"
version = '1.0.0'
from components.release
}
}
repositories {
maven {
// Declare cnbArtifactsGradlePassword in gradle.properties or export CNB_TOKEN=${YOUR_TOKEN}
def cnbArtifactsGradlePassword =
System.getenv('CNB_TOKEN') ?: project.findProperty('cnbArtifactsGradlePassword')
// Example
// url = uri("https://maven.cnb.cool/cnb/maven-repo/-/packages/")
url = uri("<REPO_URL>")
credentials {
username = 'cnb'
password = cnbArtifactsGradlePassword.toString()
}
}
}
}
}- Execute the following command.
./gradlew publishFAQ
After overwriting an existing version, the dependency build still uses the pre-overwritten version?
This is Maven's default behavior: if the package exists in the local cache, it will not be fetched from the remote repository. There are three solutions:
- Use the
--update-snapshotscommand to force Maven to check for updates of snapshot dependencies in the remote repository. This strategy only works for SNAPSHOT versions.
mvn clean package -U- Delete the local cache
# macOS and Linux default location: ~/.m2
# Windows default location: C:\Users\{username}\.m2\repository
# Or query with the following command:
mvn help:evaluate -Dexpression=settings.localRepository- Set the
updatePolicytoalwaysin settings.xml for the dependency repository. This strategy only works for SNAPSHOT versions.
<settings>
<profiles>
<profile>
<id>cnb-maven-repo-profile</id>
<repositories>
<repository>
<id>cnb-maven-repo</id>
<url><REPO_URL></url>
<!-- Configure snapshot pull policy -->
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</settings>Encountered 409 Forbidden?
You have already uploaded this package and overwriting is prohibited. To overwrite, go to the corresponding artifact repository → Artifact Settings → Policy Management:
- To overwrite only SNAPSHOT versions, select
Maven Snapshot Strategy. - To overwrite both SNAPSHOT and RELEASE versions, select
Allow overwriting of all existing versions.
More Usage
For more Maven usage, refer to the official Maven documentation.