๐๏ธ 1. Ensure Your GitLab Project Is Maven-Ready
To store your Java packages (JAR/WAR) into the GitLab Package Registry using GitLab CI/CD (SaaS, version 18.x), you can use GitLabโs built-in Maven repository support.
Hereโs a step-by-step guide to:
โ
Build your Java project
โ
Upload the .jar or .war to GitLabโs Maven Package Registry
โ
Do this all through your .gitlab-ci.yml pipeline
In your project root:
Ensure pom.xml (for Maven) is set up with:
<project>
...
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
...
</project>
Code language: HTML, XML (xml)
๐งช 2. GitLab Package Registry URL
GitLab SaaS Maven registry endpoint:
https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/maven
Code language: HTML, XML (xml)
You can find <PROJECT_ID> in your GitLab project settings or by using:
https://gitlab.com/<namespace>/<project_name>
Code language: HTML, XML (xml)
๐ 3. Create a CI/CD Job to Publish Artifact
.gitlab-ci.yml example for Maven:
stages:
- build
- publish
variables:
MAVEN_CLI_OPTS: "-B -DskipTests"
build-job:
stage: build
image: maven:3.8.7-jdk-11
script:
- mvn $MAVEN_CLI_OPTS clean package
artifacts:
paths:
- target/*.jar
- target/*.war
publish-job:
stage: publish
image: maven:3.8.7-jdk-11
script:
- mvn deploy -s settings.xml
only:
- main
Code language: PHP (php)
โ๏ธ 4. Add settings.xml for GitLab Auth
In the root of your repo (or dynamically generate in CI):
<settings>
<servers>
<server>
<id>gitlab</id>
<username>gitlab-ci-token</username>
<password>${env.CI_JOB_TOKEN}</password>
</server>
</servers>
</settings>
Code language: HTML, XML (xml)
Ensure your pom.xml contains:
<distributionManagement>
<repository>
<id>gitlab</id>
<url>https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/maven</url>
</repository>
</distributionManagement>
Code language: HTML, XML (xml)
๐ก๏ธ 5. Use the CI_JOB_TOKEN Securely
gitlab-ci-tokenis a special user recognized by GitLabโs package registry.CI_JOB_TOKENis auto-injected in GitLab CI/CD and scoped to your project.
๐ฆ 6. Check Published Packages
After the pipeline completes:
- Go to your GitLab project
- Click on Packages & Registries โ Package Registry
- Youโll see your
.jaror.warlisted
๐ง Bonus: Clean Up or Promote Packages
You can add manual jobs to:
- Promote packages to another environment
- Delete old versions via API
- Install them in another Java project using Maven:
<dependency>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
</dependency>
Code language: HTML, XML (xml)
โ Summary
| Step | Description |
|---|---|
| Build Artifact | Use mvn package in CI job |
| Upload to GitLab | Use mvn deploy with GitLab Maven repo |
| Auth | Use CI_JOB_TOKEN with gitlab-ci-token |
| View | Packages tab in GitLab UI |
How to use Package registry using Gradle
Here is a step-by-step guide to publish JAR/WAR packages from a Gradle-based Java project to GitLabโs Maven Package Registry, using GitLab SaaS 18.x.
โ Overview
- Youโll use GitLabโs Maven-compatible Package Registry
- Youโll configure Gradle to use GitLab as a Maven repo
- Youโll automate it using
.gitlab-ci.ymlandCI_JOB_TOKEN
๐ 1. Setup Your Gradle Project
In your build.gradle file:
plugins {
id 'java'
id 'maven-publish'
}
group = 'com.example'
version = '1.0.0'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
name = "GitLab"
url = uri("https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/maven")
credentials {
username = project.findProperty("gitlabUser") ?: System.getenv("CI_JOB_TOKEN")
password = project.findProperty("gitlabToken") ?: System.getenv("CI_JOB_TOKEN")
}
}
}
}
Code language: JavaScript (javascript)
Replace
<PROJECT_ID>with your GitLab project ID. You can find it on the project homepage (e.g.,gitlab.com/api/v4/projects/1234567).
๐ 2. No Hardcoded Credentials
Do not store username/token in code.
In CI/CD, GitLab will inject CI_JOB_TOKEN automatically. For local testing, you can override via gradle.properties or CLI:
gitlabUser=gitlab-ci-token
gitlabToken=<your personal access token>
Code language: HTML, XML (xml)
๐งช 3. Test Locally (Optional)
./gradlew publish \
-PgitlabUser=gitlab-ci-token \
-PgitlabToken=<your GitLab personal access token>
Code language: HTML, XML (xml)
โ๏ธ 4. Create .gitlab-ci.yml
stages:
- build
- publish
variables:
GRADLE_USER_HOME: "$CI_PROJECT_DIR/.gradle"
build-job:
stage: build
image: gradle:8.2.1-jdk17
script:
- gradle build
publish-job:
stage: publish
image: gradle:8.2.1-jdk17
script:
- gradle publish
only:
- main
Code language: JavaScript (javascript)
This will publish the JAR/WAR file to GitLab Package Registry on
mainbranch only.
๐ฆ 5. Where to View Packages
After successful pipeline execution:
- Navigate to Your Project โ Packages & Registries โ Package Registry
- Youโll see the
.jaror.warunder your published Maven group.
๐ 6. How to Use in Other Projects
In another Gradle project, consume the published package:
repositories {
maven {
url = uri("https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/maven")
credentials {
username = 'gitlab-ci-token'
password = System.getenv("CI_JOB_TOKEN") // or use .env or gradle.properties
}
}
}
dependencies {
implementation 'com.example:your-artifact:1.0.0'
}
Code language: JavaScript (javascript)
โ Summary Table
| Step | Action |
|---|---|
build.gradle | Add maven-publish, configure GitLab Maven repo |
| Credentials | Use CI_JOB_TOKEN with gitlab-ci-token |
.gitlab-ci.yml | Run gradle publish from CI/CD |
| Registry URL | https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/maven |
| View Packages | Project โ Packages & Registries โ Package Registry |
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals