Maven Artifact Registry
About 1637 wordsAbout 5 min
Create Artifact Registry
Refer to Create Artifact Registry
Obtain Artifact Registry Address
Refer to Obtain Artifact Registry Address
Create Access Token
All resources in the Cloud Native Build platform (including repositories, Artifact Registry, OPENAPI, etc.) require an access token for operations. Create Access Token
Local Development
Configure Credentials
Copy the following content to the settings.xml file, replace <YOUR_TOKEN>
with your token, and <REPO_URL>
with the Artifact Registry address.
<settings>
<servers>
<server>
<id>cnb-maven</id>
<username>cnb</username>
<!-- Replace with your access token -->
<password><YOUR_TOKEN></password>
</server>
</servers>
<profiles>
<profile>
<id>cnb-maven-profile</id>
<repositories>
<repository>
<!-- Must match the server's id -->
<id>cnb-maven</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, replace
<REPO_URL>
with the Artifact Registry address.
// build.gradle.kts
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()
}
}
}
}
// build.gradle
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.
// build.gradle.kts
dependencies {
implementation("[GROUP_ID]:[ARTIFACT_ID]:[VERSION]")
}
// Example
// dependencies {
// implementation("com.google.guava:guava:32.1.3-jre")
// }
// 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-dependencies
Push 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</id>
<name>cnb-maven</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.
// 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"])
}
}
}
// 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 publish
Cloud Native Build
Configure Credentials
Cloud Native Build supports three ways to use tokens. Refer to Using Tokens in Cloud Native Build and Development. Below are the specific client usage methods:
Replace <REPO_URL>
with the Artifact Registry address.
<settings>
<servers>
<server>
<id>cnb-maven</id>
<username>cnb</username>
<!-- Method 1: Use CNB_TOKEN -->
<password>${env.CNB_TOKEN}</password>
<!-- Method 2: Directly use, replace <your_token> with your token -->
<!--<password><your_token></password>-->
<!-- Method 3: Secret store, replace <ENV_NAME> with your secret store variable -->
<!--<password>${env.<ENV_NAME>}</password>-->
</server>
</servers>
<profiles>
<profile>
<id>cnb-maven-profile</id>
<repositories>
<repository>
<!-- Must match the server's id -->
<id>cnb-maven</id>
<!-- 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>
Replace <REPO_URL>
with the Artifact Registry address.
publishing {
repositories {
maven {
// Method 1: Use CNB_TOKEN
val cnbArtifactsGradlePassword = System.getenv("CNB_TOKEN")
// Method 2: Directly use, replace <your_token> with your token
// val cnbArtifactsGradlePassword = "<your_token>"
// Method 3: Secret store, replace <ENV_NAME> with your secret store variable
// val cnbArtifactsGradlePassword = System.getenv("ENV_NAME")
// Example
// url = uri("https://maven.cnb.cool/cnb/maven_repo/-/packages/")
url = uri("<REPO_URL>")
credentials {
username = "cnb"
password = cnbArtifactsGradlePassword.toString()
}
}
}
}
publishing {
repositories {
maven {
// Method 1: Use CNB_TOKEN
def cnbArtifactsGradlePassword = System.getenv("CNB_TOKEN")
// Method 2: Directly use, replace <your_token> with your token
// def cnbArtifactsGradlePassword = '<your_token>'
// Method 3: Secret store, replace <ENV_NAME> with your secret store variable
// def cnbArtifactsGradlePassword = System.getenv("ENV_NAME")
// Example
// url = uri("https://maven.cnb.cool/cnb/maven_repo/-/packages/")
url = uri('<REPO_URL>')
credentials {
username = 'cnb'
password = cnbArtifactsGradlePassword.toString()
}
}
}
}
Configure Development Image
$:
vscode:
- docker:
image: maven:3.8.6-openjdk-8
Pull Artifacts
Paste the following content into .cnb.yml:
main:
push:
- docker:
image: maven:3.8.6-openjdk-8
stages:
- name: mvn package
script:
mvn clean install -s ./settings.xml
main:
push:
- docker:
image: gradle:7.6.6-jdk17
stages:
- name: gradle build and run
script:
- ./gradlew build --refresh-dependencies
Push Artifacts
Paste the following content into .cnb.yml:
main:
push:
- docker:
image: maven:3.8.6-openjdk-8
stages:
- name: mvn package
script:
mvn clean deploy -s ./settings.xml
main:
push:
- docker:
image: gradle:7.6.6-jdk17
stages:
- name: gradle publish
script:
- ./gradlew publish
Workspaces
Configure Credentials
Same as Cloud Native Build Configure Credentials
Configure Development Image
$:
vscode:
- docker:
image: maven:3.8.6-openjdk-8
Pull Artifacts
Same as Local Development Pull Artifacts
Push Artifacts
Same as Local Development Push Artifacts
Examples
Gradle Local Push APK Package
- Configure the android-application plugin in gradle/libs.versions.toml.
[versions]
agp = "8.11.1"
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
- Configure the maven-publish plugin and apk packaging plugin in build.gradle.
// build.gradle.kts
plugins {
id("com.android.application") // APK packaging plugin
id("maven-publish")
}
// build.gradle
plugins {
id 'com.android.application' // APK packaging plugin
id 'maven-publish'
}
- Configure the build task and push address in build.gradle.
// build.gradle.kts
afterEvaluate {
publishing {
publications {
create<MavenPublication>("releaseApk") {
groupId = "cnb"
artifactId = "artifact"
version = "1.0.0"
// For signed apps, use outputs/apk/release/${project.name}-release.apk
val apkFile = layout.buildDirectory.file(
"outputs/apk/release/${project.name}-release-unsigned.apk"
).get().asFile
artifact(apkFile) {
builtBy(tasks.named("assembleRelease"))
classifier = "release"
extension = "apk"
}
}
}
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 {
releaseApk(MavenPublication) {
groupId = 'cnb'
artifactId = "artifact"
version = '1.0.0'
// For signed apps, use outputs/apk/release/${project.name}-release.apk
def apkFile = layout.buildDirectory.file(
'outputs/apk/release/${project.name}-release-unsigned.apk'
).get().asFile
artifact(apkFile) {
builtBy tasks.assembleRelease
classifier = 'release'
extension = 'apk'
}
}
}
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 = '<REPO_URL>'
credentials {
username = 'cnb'
password = cnbArtifactsGradlePassword.toString()
}
}
}
}
}
- Execute the following command.
./gradlew publish
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.
// 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.
// 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 publish
FAQ
After overwriting an existing version, the dependency build still uses the pre-overwritten version?
This is a Maven mechanism. If the local cache exists, it won't pull from the remote repository. There are three solutions:
- Use the
--update-snapshots
command 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
- Remove the cached package by deleting the local Maven 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
updatePolicy
toalways
in settings.xml for the dependency repository. This strategy only works for SNAPSHOT versions.
<settings>
<profiles>
<profile>
<id>cnb-maven-profile</id>
<repositories>
<repository>
<id>cnb-maven</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 the package, go to the corresponding Artifact Registry -> Registry Settings -> Strategy:
- 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.