-[Container Registry](../user/packages/container_registry/index.md)(`$CI_REGISTRY_PASSWORD` is actually `$CI_JOB_TOKEN`, but this may change in the future)
[defined in `settings.xml`](#add-the-package-registry-as-a-maven-remote).
Replace `PROJECT_ID` with your project ID which can be found on the home page
of your project.
If you have a self-managed GitLab installation, replace `gitlab.com` with your
domain name.
- The `id` is what you [defined in `settings.xml`](#authenticate-to-the-package-registry-with-maven).
- The `PROJECT_ID` is your project ID, which you can view on your project's home page.
- Replace `gitlab.example.com` with your domain name.
- For retrieving artifacts, use either the
[URL-encoded](../../../api/README.md#namespaced-path-encoding) path of the project
(like `group%2Fproject`) or the project's ID (like `42`). However, only the
project's ID can be used for publishing.
NOTE: **Note:**
For retrieving artifacts, you can use either the
[URL encoded](../../../api/README.md#namespaced-path-encoding) path of the project
(such as `group%2Fproject`) or the project's ID (such as `42`). However, only the
project's ID can be used for uploading.
## Publish a package
## Uploading packages
After you have set up the [remote and authentication](#authenticate-to-the-package-registry-with-maven)
and [configured your project](#use-the-gitlab-endpoint-for-maven-packages),
publish a Maven artifact from your project.
Once you have set up the [remote and authentication](#add-the-package-registry-as-a-maven-remote)
and [configured your project](#configuring-your-project-to-use-the-gitlab-maven-repository-url),
test to upload a Maven artifact from a project of yours.
### Publish by using Maven
### Upload using Maven
To publish a package by using Maven:
```shell
mvn deploy
```
If the deploy is successful, you should see the build success message again:
If the deploy is successful, the build success message should be displayed:
```shell
...
...
...
@@ -585,108 +551,112 @@ If the deploy is successful, you should see the build success message again:
...
```
You should also see that the upload was uploaded to the correct registry:
The message should also show that the package was published to the correct location:
```shell
Uploading to gitlab-maven: https://gitlab.com/api/v4/projects/PROJECT_ID/packages/maven/com/mycompany/mydepartment/my-project/1.0-SNAPSHOT/my-project-1.0-20200128.120857-1.jar
Uploading to gitlab-maven: https://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.jar
```
### Upload using Gradle
### Publish by using Gradle
Add the Gradle plugin [`maven-publish`](https://docs.gradle.org/current/userguide/publishing_maven.html) to the plugins section:
To publish a package by using Gradle:
```groovy
plugins {
id 'java'
id 'maven-publish'
}
```
1. Add the Gradle plugin [`maven-publish`](https://docs.gradle.org/current/userguide/publishing_maven.html) to the plugins section:
value = gitLabPrivateToken // the variable resides in ~/.gradle/gradle.properties
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
```
Replace `PROJECT_ID` with your project ID which can be found on the home page
of your project.
1. Replace `PROJECT_ID` with your project ID, which can be found on your project's home page.
Run the publish task:
1. Run the publish task:
```shell
gradle publish
```
```shell
gradle publish
```
You can then navigate to your project's **Packages & Registries** page and see the uploaded
artifacts or even delete them.
Now navigate to your project's **Packages & Registries** page and view the published artifacts.
## Installing a package
## Install a package
Installing a package from the GitLab Package Registry requires that you set up
the [remote and authentication](#add-the-package-registry-as-a-maven-remote)
as above. Once this is completed, there are two ways to install a package.
To install a package from the GitLab Package Registry, you must configure
the [remote and authenticate](#authenticate-to-the-package-registry-with-maven).
When this is completed, there are two ways to install a package.
### Install using Maven with `mvn install`
### Use Maven with `mvn install`
Add the dependency manually to your project `pom.xml` file. To add the example
created above, the XML would look like:
To install a package by using `mvn install`:
```xml
<dependency>
<groupId>com.mycompany.mydepartment</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
1. Add the dependency manually to your project `pom.xml` file.
To add the example created earlier, the XML would be:
Then, inside your project, run the following:
```xml
<dependency>
<groupId>com.mycompany.mydepartment</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
```shell
mvn install
```
1. In your project, run the following:
```shell
mvn install
```
Provided everything is set up correctly, you should see the dependency
downloaded from the GitLab Package Registry:
The message should show that the package is downloading from the Package Registry:
```shell
Downloading from gitlab-maven: http://gitlab.com/api/v4/projects/PROJECT_ID/packages/maven/com/mycompany/mydepartment/my-project/1.0-SNAPSHOT/my-project-1.0-20200128.120857-1.pom
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
```
### Install using Maven with `mvn dependency:get`
### Use Maven with `mvn dependency:get`
The second way to install packages is to use the Maven commands directly.
Inside your project directory, run:
You can install packages by using the Maven commands directly.
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
```
You should see the same downloading message confirming that the project was
retrieved from the GitLab Package Registry.
TIP: **Tip:**
Both the XML block and Maven command are readily copy and pastable from the
Package details page, allowing for quick and easy installation.
In the GitLab UI, on the Package Registry page for Maven, you can view and copy these commands.
### Install using Gradle
### Use Gradle
Add a [dependency](https://docs.gradle.org/current/userguide/declaring_dependencies.html) to `build.gradle` in the dependencies section:
...
...
@@ -696,25 +666,25 @@ dependencies {
}
```
## Removing a package
## Remove a package
In the packages view of your project page, you can delete packages by clicking
the red trash icons or by clicking the **Delete** button on the package details
page.
For your project, go to **Packages & Registries > Package Registry**.
## Creating Maven packages with GitLab CI/CD
To remove a package, click the red trash icon or, from the package details, the **Delete** button.
Once you have your repository configured to use the GitLab Maven Repository,
## Create Maven packages with GitLab CI/CD
After you have configured your repository to use the Package Repository for Maven,
you can configure GitLab CI/CD to build new packages automatically.
### Creating Maven packages with GitLab CI/CD using Maven
### Create Maven packages with GitLab CI/CD by using Maven
The example below shows how to create a new package each time the `master` branch
is updated:
You can create a new package each time the `master` branch is updated.
1. Create a `ci_settings.xml` file that serves as Maven's `settings.xml` file.
Add the server section with the same ID you defined in your `pom.xml` file.
For example, in our case it's `gitlab-maven`:
1. Add the `server` section with the same ID you defined in your `pom.xml` file.
- The user is `root`, because the job runs in a Docker container.
- Maven uses the configured CI [environment variables](../../../ci/variables/README.md#predefined-environment-variables).
### Creating Maven packages with GitLab CI/CD using Gradle
### Create Maven packages with GitLab CI/CD by using Gradle
The example below shows how to create a new package each time the `master` branch
is updated:
You can create a package each time the `master` branch
is updated.
1. Make sure you use the Job-Token authentication as described in ["Authenticating with a CI job token in Gradle"](#authenticate-with-a-ci-job-token-in-gradle).
1. Authenticate with [a CI job token in Gradle](#authenticate-with-a-ci-job-token-in-gradle).
1. Add a `deploy` job to your `.gitlab-ci.yml` file:
...
...
@@ -795,11 +765,13 @@ is updated:
- master
```
1. Push those files to your repository.
1. Commit files to your repository.
When the pipeline is successful, the package is created.
### Version validation
The version string is validated using the following regex.
The version string is validated by using the following regex.
```ruby
\A(\.?[\w\+-]+\.?)+\z
...
...
@@ -827,7 +799,7 @@ When you set these options, all network requests are logged and a large amount o
### Useful Maven command-line options
There are some [Maven command-line options](https://maven.apache.org/ref/current/maven-embedder/cli.html)
that may be useful when performing tasks with GitLab CI/CD.
that you can use when performing tasks with GitLab CI/CD.
- File transfer progress can make the CI logs hard to read.
Option `-ntp,--no-transfer-progress` was added in
...
...
@@ -835,7 +807,7 @@ that may be useful when performing tasks with GitLab CI/CD.
@@ -75,8 +75,8 @@ you do this, you can push your NPM package to your project using `npm publish`,
If you are using Maven, this involves updating your `pom.xml` file with distribution sections, including the
appropriate URL for your project, as described in the [GitLab Maven Repository documentation](../maven_repository/index.md#project-level-maven-endpoint).
Then, you need to add a `settings.xml` file and [include your access token](../maven_repository/index.md#authenticate-with-a-personal-access-token).
Now you can [deploy Maven packages](../maven_repository/index.md#uploading-packages) to your project.
Then, you need to add a `settings.xml` file and [include your access token](../maven_repository/index.md#authenticate-with-a-personal-access-token-in-maven).
Now you can [deploy Maven packages](../maven_repository/index.md#publish-a-package) to your project.