debian-mirror-gitlab/doc/user/packages/maven_repository/index.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

756 lines
25 KiB
Markdown
Raw Normal View History

2020-06-23 00:09:42 +05:30
---
stage: Package
2023-01-13 00:05:48 +05:30
group: Package Registry
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-06-23 00:09:42 +05:30
---
2023-03-04 22:38:38 +05:30
# Maven packages in the Package Registry **(FREE)**
2019-12-04 20:38:33 +05:30
2021-04-29 21:17:54 +05:30
Publish [Maven](https://maven.apache.org) artifacts in your project's Package Registry.
2021-01-03 14:25:43 +05:30
Then, install the packages whenever you need to use them as a dependency.
2019-12-04 20:38:33 +05:30
2021-04-29 21:17:54 +05:30
For documentation of the specific API endpoints that the Maven package manager
client uses, see the [Maven API documentation](../../../api/packages/maven.md).
2023-07-09 08:55:56 +05:30
Supported clients:
- `mvn`. Learn how to build a [Maven](../workflows/build_packages.md#maven) package.
- `gradle`. Learn how to build a [Gradle](../workflows/build_packages.md#gradle) package.
- `sbt`.
- `sbt` can only be used to [pull dependencies](#install-a-package).
See this [issue 408479](https://gitlab.com/gitlab-org/gitlab/-/issues/408479) for more details.
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
## Publish to the GitLab Package Registry
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
### Authenticate to the Package Registry
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
You need an token to publish a package. There are different tokens available depending on what you're trying to achieve. For more information, review the [guidance on tokens](../package_registry/index.md#authenticate-with-the-registry).
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
Create a token and save it to use later in the process.
2020-05-24 23:13:21 +05:30
2023-06-20 00:43:36 +05:30
Do not use authentication methods other than the methods documented here. Undocumented authentication methods might be removed in the future.
2023-07-09 08:55:56 +05:30
#### Edit the client configuration
2020-05-24 23:13:21 +05:30
2023-07-09 08:55:56 +05:30
You must add the authentication details to the configuration file
for your client.
2020-05-24 23:13:21 +05:30
2023-07-09 08:55:56 +05:30
::Tabs
:::TabTitle `mvn`
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
| Token type | Name must be | Token |
| --------------------- | --------------- | ---------------------------------------------------------------------- |
| Personal access token | `Private-Token` | Paste token as-is, or define an environment variable to hold the token |
| Deploy token | `Deploy-Token` | Paste token as-is, or define an environment variable to hold the token |
| CI Job token | `Job-Token` | `${CI_JOB_TOKEN}` |
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
NOTE:
The `<name>` field must be named to match the token you chose.
Add the following section to your
[`settings.xml`](https://maven.apache.org/settings.html) file.
2019-12-04 20:38:33 +05:30
```xml
<settings>
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
2023-03-04 22:38:38 +05:30
<name>REPLACE_WITH_NAME</name>
<value>REPLACE_WITH_TOKEN</value>
2019-12-04 20:38:33 +05:30
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
```
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
| Token type | Name must be | Token |
| --------------------- | --------------- | ---------------------------------------------------------------------- |
| Personal access token | `Private-Token` | Paste token as-is, or define an environment variable to hold the token |
| Deploy token | `Deploy-Token` | Paste token as-is, or define an environment variable to hold the token |
| CI Job token | `Job-Token` | `System.getenv("CI_JOB_TOKEN")` |
NOTE:
The `<name>` field must be named to match the token you chose.
In [your `GRADLE_USER_HOME` directory](https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home),
create a file `gradle.properties` with the following content:
```properties
gitLabPrivateToken=REPLACE_WITH_YOUR_TOKEN
```
Add a `repositories` section to your
[`build.gradle`](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html)
file:
- In Groovy DSL:
```groovy
repositories {
maven {
url "https://gitlab.example.com/api/v4/groups/<group>/-/packages/maven"
name "GitLab"
credentials(HttpHeaderCredentials) {
name = 'REPLACE_WITH_NAME'
value = gitLabPrivateToken
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
```
- In Kotlin DSL:
```kotlin
repositories {
maven {
url = uri("https://gitlab.example.com/api/v4/groups/<group>/-/packages/maven")
name = "GitLab"
credentials(HttpHeaderCredentials::class) {
name = "REPLACE_WITH_NAME"
value = findProperty("gitLabPrivateToken") as String?
}
authentication {
create("header", HttpHeaderAuthentication::class)
}
}
}
```
:::TabTitle `sbt`
| Token type | Name must be | Token |
|-----------------------|------------------------------|------------------------------------------------------------------------|
| Personal access token | The username of the user | Paste token as-is, or define an environment variable to hold the token |
| Deploy token | The username of deploy token | Paste token as-is, or define an environment variable to hold the token |
| CI Job token | `gitlab-ci-token` | `sys.env.get("CI_JOB_TOKEN").get` |
Authentication for [SBT](https://www.scala-sbt.org/index.html) is based on
[basic HTTP Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
You must to provide a name and a password.
NOTE:
The name field must be named to match the token you chose.
To install a package from the Maven GitLab Package Registry by using `sbt`, you must configure
a [Maven resolver](https://www.scala-sbt.org/1.x/docs/Resolvers.html#Maven+resolvers).
If you're accessing a private or an internal project or group, you need to set up
[credentials](https://www.scala-sbt.org/1.x/docs/Publishing.html#Credentials).
After configuring the resolver and authentication, you can install a package
from a project, group, or namespace.
In your [`build.sbt`](https://www.scala-sbt.org/1.x/docs/Directories.html#sbt+build+definition+files), add the following lines:
```scala
resolvers += ("gitlab" at "<endpoint url>")
credentials += Credentials("GitLab Packages Registry", "<host>", "<name>", "<token>")
```
In this example:
- `<endpoint url>` is the [endpoint URL](#endpoint-urls).
Example: `https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven`.
- `<host>` is the host present in the `<endpoint url>` without the protocol
scheme or the port. Example: `gitlab.example.com`.
- `<name>` and `<token>` are explained in the table above.
::EndTabs
2023-03-04 22:38:38 +05:30
### Naming convention
2022-11-25 23:54:43 +05:30
2023-03-04 22:38:38 +05:30
You can use one of three endpoints to install a Maven package. You must publish a package to a project, but the endpoint you choose determines the settings you add to your `pom.xml` file for publishing.
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
The three endpoints are:
2022-11-25 23:54:43 +05:30
2023-03-04 22:38:38 +05:30
- **Project-level**: Use when you have a few Maven packages and they are not in the same GitLab group.
- **Group-level**: Use when you want to install packages from many different projects in the same GitLab group. GitLab does not guarantee the uniqueness of package names within the group. You can have two projects with the same package name and package version. As a result, GitLab serves whichever one is more recent.
- **Instance-level**: Use when you have many packages in different GitLab groups or in their own namespace.
2020-05-24 23:13:21 +05:30
2023-06-20 00:43:36 +05:30
For the instance-level endpoint, ensure the relevant section of your `pom.xml` in Maven looks like this:
```xml
<groupId>group-slug.subgroup-slug</groupId>
<artifactId>project-slug</artifactId>
```
2023-03-04 22:38:38 +05:30
**Only packages that have the same path as the project** are exposed by the instance-level endpoint.
2022-11-25 23:54:43 +05:30
2023-03-04 22:38:38 +05:30
| Project | Package | Instance-level endpoint available |
| ------------------- | -------------------------------- | --------------------------------- |
| `foo/bar` | `foo/bar/1.0-SNAPSHOT` | Yes |
| `gitlab-org/gitlab` | `foo/bar/1.0-SNAPSHOT` | No |
| `gitlab-org/gitlab` | `gitlab-org/gitlab/1.0-SNAPSHOT` | Yes |
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
#### Endpoint URLs
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
| Endpoint | Endpoint URL for `pom.xml` | Additional information |
| -------- | ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
| Project | `https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven` | Replace `gitlab.example.com` with your domain name. Replace `<project_id>` with your project ID, found on your project's homepage. |
| Group | `https://gitlab.example.com/api/v4/groups/<group_id>/-/packages/maven` | Replace `gitlab.example.com` with your domain name. Replace `<group_id>` with your group ID, found on your group's homepage. |
2023-03-17 16:20:25 +05:30
| Instance | `https://gitlab.example.com/api/v4/packages/maven` | Replace `gitlab.example.com` with your domain name. |
2021-01-29 00:20:46 +05:30
2023-07-09 08:55:56 +05:30
### Edit the configuration file for publishing
You must add publishing details to the configuration file for your client.
::Tabs
:::TabTitle `mvn`
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
No matter which endpoint you choose, you must have:
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
- A project-specific URL in the `distributionManagement` section.
- A `repository` and `distributionManagement` section.
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
The relevant `repository` section of your `pom.xml` in Maven should look like this:
2019-12-04 20:38:33 +05:30
```xml
<repositories>
<repository>
<id>gitlab-maven</id>
2023-03-04 22:38:38 +05:30
<url><your_endpoint_url></url>
2019-12-04 20:38:33 +05:30
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
2023-03-04 22:38:38 +05:30
<url>https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven</url>
2019-12-04 20:38:33 +05:30
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
2023-03-04 22:38:38 +05:30
<url>https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven</url>
2019-12-04 20:38:33 +05:30
</snapshotRepository>
</distributionManagement>
```
2023-07-09 08:55:56 +05:30
- The `id` is what you [defined in `settings.xml`](#edit-the-client-configuration).
2023-03-04 22:38:38 +05:30
- The `<your_endpoint_url>` depends on which [endpoint](#endpoint-urls) you choose.
2021-01-29 00:20:46 +05:30
- Replace `gitlab.example.com` with your domain name.
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
To publish a package by using Gradle:
1. Add the Gradle plugin [`maven-publish`](https://docs.gradle.org/current/userguide/publishing_maven.html) to the plugins section:
- In Groovy DSL:
```groovy
plugins {
id 'java'
id 'maven-publish'
}
```
- In Kotlin DSL:
```kotlin
plugins {
java
`maven-publish`
}
```
1. Add a `publishing` section:
- In Groovy DSL:
```groovy
publishing {
publications {
library(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "https://gitlab.example.com/api/v4/projects/<PROJECT_ID>/packages/maven"
credentials(HttpHeaderCredentials) {
name = "REPLACE_WITH_TOKEN_NAME"
value = gitLabPrivateToken // the variable resides in $GRADLE_USER_HOME/gradle.properties
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
```
- In Kotlin DSL:
```kotlin
publishing {
publications {
create<MavenPublication>("library") {
from(components["java"])
}
}
repositories {
maven {
url = uri("https://gitlab.example.com/api/v4/projects/<PROJECT_ID>/packages/maven")
credentials(HttpHeaderCredentials::class) {
name = "REPLACE_WITH_TOKEN_NAME"
value =
findProperty("gitLabPrivateToken") as String? // the variable resides in $GRADLE_USER_HOME/gradle.properties
}
authentication {
create("header", HttpHeaderAuthentication::class)
}
}
}
}
```
::EndTabs
2021-01-29 00:20:46 +05:30
## Publish a package
2019-12-04 20:38:33 +05:30
2023-03-04 22:38:38 +05:30
After you have set up the [authentication](#authenticate-to-the-package-registry)
and [chosen an endpoint for publishing](#naming-convention),
2021-03-11 19:13:27 +05:30
publish a Maven package to your project.
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
::Tabs
:::TabTitle `mvn`
2021-01-29 00:20:46 +05:30
To publish a package by using Maven:
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-12-04 20:38:33 +05:30
mvn deploy
```
2021-01-29 00:20:46 +05:30
If the deploy is successful, the build success message should be displayed:
2020-03-13 15:44:24 +05:30
```shell
...
[INFO] BUILD SUCCESS
...
```
2021-01-29 00:20:46 +05:30
The message should also show that the package was published to the correct location:
2020-03-13 15:44:24 +05:30
```shell
2023-03-04 22:38:38 +05:30
Uploading to gitlab-maven: https://example.com/api/v4/projects/PROJECT_ID/packages/maven/com/mycompany/mydepartment/my-project/1.0-SNAPSHOT/my-project-1.0-20200128.120857-1.jar
2020-03-13 15:44:24 +05:30
```
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
Run the publish task:
```shell
gradle publish
```
Go to your project's **Packages and registries** page and view the published packages.
::EndTabs
2021-01-29 00:20:46 +05:30
## Install a package
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
To install a package from the GitLab Package Registry, you must configure
2023-03-04 22:38:38 +05:30
the [remote and authenticate](#authenticate-to-the-package-registry).
2021-03-11 19:13:27 +05:30
When this is completed, you can install a package from a project,
group, or namespace.
If multiple packages have the same name and version, when you install
a package, the most recently-published package is retrieved.
2020-03-13 15:44:24 +05:30
2023-07-09 08:55:56 +05:30
::Tabs
:::TabTitle `mvn`
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
To install a package by using `mvn install`:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
1. Add the dependency manually to your project `pom.xml` file.
To add the example created earlier, the XML would be:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
```xml
<dependency>
<groupId>com.mycompany.mydepartment</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
1. In your project, run the following:
```shell
mvn install
```
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
The message should show that the package is downloading from the Package Registry:
2020-03-13 15:44:24 +05:30
```shell
2021-01-29 00:20:46 +05:30
Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/PROJECT_ID/packages/maven/com/mycompany/mydepartment/my-project/1.0-SNAPSHOT/my-project-1.0-20200128.120857-1.pom
2020-03-13 15:44:24 +05:30
```
2023-07-09 08:55:56 +05:30
You can also install packages by using the Maven [`dependency:get` command](https://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html) directly.
2021-01-29 00:20:46 +05:30
1. In your project directory, run:
```shell
2022-10-11 01:57:18 +05:30
mvn dependency:get -Dartifact=com.nickkipling.app:nick-test-app:1.1-SNAPSHOT -DremoteRepositories=gitlab-maven::::<gitlab endpoint url> -s <path to settings.xml>
2021-01-29 00:20:46 +05:30
```
2023-03-04 22:38:38 +05:30
- `<gitlab endpoint url>` is the URL of the GitLab [endpoint](#endpoint-urls).
2023-07-09 08:55:56 +05:30
- `<path to settings.xml>` is the path to the `settings.xml` file that contains the [authentication details](#edit-the-client-configuration).
2022-10-11 01:57:18 +05:30
NOTE:
The repository IDs in the command(`gitlab-maven`) and the `settings.xml` file must match.
2021-01-29 00:20:46 +05:30
The message should show that the package is downloading from the Package Registry:
2020-03-13 15:44:24 +05:30
```shell
2021-01-29 00:20:46 +05:30
Downloading from gitlab-maven: http://gitlab.example.com/api/v4/projects/PROJECT_ID/packages/maven/com/mycompany/mydepartment/my-project/1.0-SNAPSHOT/my-project-1.0-20200128.120857-1.pom
2020-03-13 15:44:24 +05:30
```
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
To install a package by using `gradle`:
1. Add a [dependency](https://docs.gradle.org/current/userguide/declaring_dependencies.html) to `build.gradle` in the dependencies section:
- In Groovy DSL:
```groovy
dependencies {
implementation 'com.mycompany.mydepartment:my-project:1.0-SNAPSHOT'
}
```
- In Kotlin DSL:
```kotlin
dependencies {
implementation("com.mycompany.mydepartment:my-project:1.0-SNAPSHOT")
}
```
1. In your project, run the following:
```shell
gradle install
```
:::TabTitle `sbt`
To install a package by using `sbt`:
1. Add an [inline dependency](https://www.scala-sbt.org/1.x/docs/Library-Management.html#Dependencies) to `build.sbt`:
```scala
libraryDependencies += "com.mycompany.mydepartment" % "my-project" % "8.4"
```
1. In your project, run the following:
```shell
sbt update
```
::EndTabs
2023-03-04 22:38:38 +05:30
## Helpful hints
2020-03-13 15:44:24 +05:30
2023-03-04 22:38:38 +05:30
### Publishing a package with the same name or version
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
When you publish a package with the same name and version as an existing package, the new package
files are added to the existing package. You can still use the UI or API to access and view the
existing package's older assets.
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
To delete older package versions, consider using the Packages API or the UI.
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
### Do not allow duplicate Maven packages
2022-11-25 23:54:43 +05:30
2023-03-04 22:38:38 +05:30
To prevent users from publishing duplicate Maven packages, you can use the [GraphQl API](../../../api/graphql/reference/index.md#packagesettings) or the UI.
2022-11-25 23:54:43 +05:30
2023-03-04 22:38:38 +05:30
In the UI:
1. For your group, go to **Settings > Packages and registries**.
1. Expand the **Package Registry** section.
1. Turn on the **Do not allow duplicates** toggle.
1. Optional. To allow some duplicate packages, in the **Exceptions** box, enter a regex pattern that matches the names and/or versions of packages you want to allow.
Your changes are automatically saved.
2022-10-11 01:57:18 +05:30
2023-03-04 22:38:38 +05:30
### Request forwarding to Maven Central
2022-10-11 01:57:18 +05:30
FLAG:
2023-03-04 22:38:38 +05:30
By default this feature is not available for self-managed. To make it available, ask an administrator to [enable the feature flag](../../../administration/feature_flags.md) named `maven_central_request_forwarding`.
This feature is not available for SaaS users.
2022-10-11 01:57:18 +05:30
When a Maven package is not found in the Package Registry, the request is forwarded
to [Maven Central](https://search.maven.org/).
When the feature flag is enabled, administrators can disable this behavior in the
[Continuous Integration settings](../../admin_area/settings/continuous_integration.md).
2023-03-04 22:38:38 +05:30
Maven forwarding is restricted to only the project level and
group level [endpoints](#naming-convention). The instance level endpoint
2022-10-11 01:57:18 +05:30
has naming restrictions that prevent it from being used for packages that don't follow that convention and also
introduces too much security risk for supply-chain style attacks.
2023-07-09 08:55:56 +05:30
#### Additional configuration for `mvn`
When using `mvn`, there are many ways to configure your Maven project so that it requests packages
in Maven Central from GitLab. Maven repositories are queried in a
[specific order](https://maven.apache.org/guides/mini/guide-multiple-repositories.html#repository-order).
By default, Maven Central is usually checked first through the
[Super POM](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Super_POM), so
GitLab needs to be configured to be queried before maven-central.
2022-10-11 01:57:18 +05:30
To ensure all package requests are sent to GitLab instead of Maven Central,
you can override Maven Central as the central repository by adding a `<mirror>`
section to your `settings.xml`:
```xml
<settings>
<servers>
<server>
<id>central-proxy</id>
<configuration>
<httpHeaders>
<property>
<name>Private-Token</name>
2023-03-04 22:38:38 +05:30
<value><personal_access_token></value>
2022-10-11 01:57:18 +05:30
</property>
</httpHeaders>
</configuration>
</server>
</servers>
<mirrors>
<mirror>
<id>central-proxy</id>
<name>GitLab proxy of central repo</name>
2023-03-04 22:38:38 +05:30
<url>https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven</url>
2022-10-11 01:57:18 +05:30
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
```
2023-03-04 22:38:38 +05:30
### Create Maven packages with GitLab CI/CD
2021-01-29 00:20:46 +05:30
After you have configured your repository to use the Package Repository for Maven,
2020-05-24 23:13:21 +05:30
you can configure GitLab CI/CD to build new packages automatically.
2023-07-09 08:55:56 +05:30
::Tabs
:::TabTitle `mvn`
2020-05-24 23:13:21 +05:30
2023-07-09 08:55:56 +05:30
You can create a new package each time the default branch is updated.
2019-12-04 20:38:33 +05:30
2020-11-24 15:15:51 +05:30
1. Create a `ci_settings.xml` file that serves as Maven's `settings.xml` file.
2021-01-29 00:20:46 +05:30
1. Add the `server` section with the same ID you defined in your `pom.xml` file.
For example, use `gitlab-maven` as the ID:
2019-12-04 20:38:33 +05:30
```xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Job-Token</name>
2022-04-04 11:22:00 +05:30
<value>${CI_JOB_TOKEN}</value>
2019-12-04 20:38:33 +05:30
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
```
2021-01-29 00:20:46 +05:30
1. Make sure your `pom.xml` file includes the following.
2021-03-11 19:13:27 +05:30
You can either let Maven use the [predefined CI/CD variables](../../../ci/variables/predefined_variables.md), as shown in this example,
2021-02-22 17:27:13 +05:30
or you can hard code your server's hostname and project's ID.
2019-12-04 20:38:33 +05:30
```xml
<repositories>
<repository>
<id>gitlab-maven</id>
2022-04-04 11:22:00 +05:30
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
2019-12-04 20:38:33 +05:30
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
2022-04-04 11:22:00 +05:30
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
2019-12-04 20:38:33 +05:30
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
2022-04-04 11:22:00 +05:30
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
2019-12-04 20:38:33 +05:30
</snapshotRepository>
</distributionManagement>
```
1. Add a `deploy` job to your `.gitlab-ci.yml` file:
```yaml
deploy:
2020-10-24 23:57:45 +05:30
image: maven:3.6-jdk-11
2019-12-04 20:38:33 +05:30
script:
- 'mvn deploy -s ci_settings.xml'
2023-07-09 08:55:56 +05:30
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
2019-12-04 20:38:33 +05:30
```
1. Push those files to your repository.
2020-11-24 15:15:51 +05:30
The next time the `deploy` job runs, it copies `ci_settings.xml` to the
2021-01-29 00:20:46 +05:30
user's home location. In this example:
- The user is `root`, because the job runs in a Docker container.
2021-03-11 19:13:27 +05:30
- Maven uses the configured CI/CD variables.
2020-03-13 15:44:24 +05:30
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
You can create a package each time the default branch
is updated.
1. Authenticate with [a CI job token in Gradle](#edit-the-client-configuration).
1. Add a `deploy` job to your `.gitlab-ci.yml` file:
```yaml
deploy:
image: gradle:6.5-jdk11
script:
- 'gradle publish'
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
```
1. Commit files to your repository.
When the pipeline is successful, the Maven package is created.
::EndTabs
2023-03-04 22:38:38 +05:30
### Version validation
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
The version string is validated by using the following regex.
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
```ruby
\A(?!.*\.\.)[\w+.-]+\z
```
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
You can experiment with the regex and try your version strings on [this regular expression editor](https://rubular.com/r/rrLQqUXjfKEoL6).
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
### Useful Maven command-line options
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
There are some [Maven command-line options](https://maven.apache.org/ref/current/maven-embedder/cli.html)
that you can use when performing tasks with GitLab CI/CD.
2021-01-29 00:20:46 +05:30
2023-03-04 22:38:38 +05:30
- File transfer progress can make the CI logs hard to read.
Option `-ntp,--no-transfer-progress` was added in
[3.6.1](https://maven.apache.org/docs/3.6.1/release-notes.html#User_visible_Changes).
Alternatively, look at `-B,--batch-mode`
[or lower level logging changes.](https://stackoverflow.com/questions/21638697/disable-maven-download-progress-indication)
2020-05-24 23:13:21 +05:30
2023-03-04 22:38:38 +05:30
- Specify where to find the `pom.xml` file (`-f,--file`):
2020-07-28 23:09:34 +05:30
2023-03-04 22:38:38 +05:30
```yaml
package:
script:
- 'mvn --no-transfer-progress -f helloworld/pom.xml package'
```
2020-07-28 23:09:34 +05:30
2023-03-04 22:38:38 +05:30
- Specify where to find the user settings (`-s,--settings`) instead of
[the default location](https://maven.apache.org/settings.html). There's also a `-gs,--global-settings` option:
```yaml
package:
script:
- 'mvn -s settings/ci.xml package'
```
2020-07-28 23:09:34 +05:30
2023-03-04 22:38:38 +05:30
### Supported CLI commands
2023-07-09 08:55:56 +05:30
The GitLab Maven repository supports the following CLI commands:
::Tabs
:::TabTitle `mvn`
2023-03-04 22:38:38 +05:30
- `mvn deploy`: Publish your package to the Package Registry.
- `mvn install`: Install packages specified in your Maven project.
- `mvn dependency:get`: Install a specific package.
2020-07-28 23:09:34 +05:30
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
- `gradle publish`: Publish your package to the Package Registry.
- `gradle install`: Install packages specified in your Gradle project.
::EndTabs
2020-03-13 15:44:24 +05:30
## Troubleshooting
2023-07-09 08:55:56 +05:30
To improve performance, clients cache files related to a package. If you encounter issues, clear
2021-09-30 23:02:18 +05:30
the cache with these commands:
2023-07-09 08:55:56 +05:30
::Tabs
:::TabTitle `mvn`
2021-09-30 23:02:18 +05:30
```shell
rm -rf ~/.m2/repository
```
2023-07-09 08:55:56 +05:30
:::TabTitle `gradle`
2021-09-30 23:02:18 +05:30
```shell
2022-06-21 17:19:12 +05:30
rm -rf ~/.gradle/caches # Or replace ~/.gradle with your custom GRADLE_USER_HOME
2021-09-30 23:02:18 +05:30
```
2023-07-09 08:55:56 +05:30
::EndTabs
2020-10-24 23:57:45 +05:30
### Review network trace logs
If you are having issues with the Maven Repository, you may want to review network trace logs.
For example, try to run `mvn deploy` locally with a PAT token and use these options:
```shell
mvn deploy \
-Dorg.slf4j.simpleLogger.log.org.apache.maven.wagon.providers.http.httpclient=trace \
-Dorg.slf4j.simpleLogger.log.org.apache.maven.wagon.providers.http.httpclient.wire=trace
```
2021-02-22 17:27:13 +05:30
WARNING:
2020-10-24 23:57:45 +05:30
When you set these options, all network requests are logged and a large amount of output is generated.
2021-01-29 00:20:46 +05:30
### Verify your Maven settings
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
If you encounter issues within CI/CD that relate to the `settings.xml` file, try adding
an additional script task or job to [verify the effective settings](https://maven.apache.org/plugins/maven-help-plugin/effective-settings-mojo.html).
2020-03-13 15:44:24 +05:30
The help plugin can also provide
[system properties](https://maven.apache.org/plugins/maven-help-plugin/system-mojo.html), including environment variables:
```yaml
mvn-settings:
script:
- 'mvn help:effective-settings'
package:
script:
- 'mvn help:system'
- 'mvn package'
```