Using GitLab as Maven Registry - Creating Maven Registry

This shows how to create a Maven Registry in GitLab.

Gitlab as Maven Registry Example

This document shows how to setup a GitLab Project as a Maven Registry.

Getting Started

For the successful completion of this example please update your operating system and if needed install the software packages listed below.

Software Prerequisites:

[note:  Please refer to LinkedIn Learning for a series of Gitlab Tutorials.]

Configure Gitlab to be a repository for the Maven artifacts

To initiate the process create a Gitlab project, get its project ID, and configure an access token.

A) Create the GitLab Project

The GitLab Project does not need a git repository. Please go to https://git.doit.wisc.edu/ to create a new Gitlab project and acquire the details: name, ID, and access token. Create a blank GitLab project:

  • Project Name: remotemavenregistryex

  • Project URL : https://git.doit.wisc.edu/sharedtools/mavenregistryex

  • Visibility: Private

(A2) After the project creation an ID is generated. The projects ID is viewable under the title in the projects home page.

Project ID

(A3) To acquire the project's Access Token please see instructions below.

SettingsAdd Token

Token

B) Create a local root directory for the newly created GitLab project.

For this example, below is the local root directory path.

$ cd /Users/username/

B2) Create a local directory to complete this example. Change "localmavenregistryex" to own project.

$ mkdir localmavenregistryex

B3) Confirm directory path.

$ cd localmavenregistryex
$ pwd
/Users/username/localmavenregistryex/

C) Generate a Maven Project Starting Point

This example uses docker to run the maven commands (and requires you have docker installed). If you have maven installed locally you may alternatively just run the mvn part of the command (mvn and everything that follows that part of the command). To create a Maven project with Docker run the following command to generate a starting point. Change "mvnexample" to the name of own project.

$ cd /Users/username/localmavenregistryex/
$ docker run -it --rm --name maven-create \
-v "$PWD":/usr/src/app \
-v "$HOME"/.m2:/root/.m2 \
-w /usr/src/app maven:3.6-jdk-8 \
mvn archetype:generate \
-DgroupId=edu.wisc.mvnexample \
-DartifactId=mvnexample \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false

D) Build the Maven Project

Run the Docker commands below to build the Maven Project.

docker run -it --rm --name maven-build \

-v "$PWD"/mvnexample:/usr/src/app \

-v "$HOME"/.m2:/root/.m2 \

-w /usr/src/app maven:3.6-jdk-8 \

mvn package

E) Edit MAVEN pom.xml

Navigate to the appropiate directory and edit the pom.xml file with the details from step (A).
Add the following to the pom.xml after the dependencies block and before the build block.


$ cd /Users/username/localmavenregistryex/mvnexample/
$ vi mvnexample/pom.xml 

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://git.doit.wisc.edu/api/v4/projects/PROJECT_ID/packages/maven</url>
    </repository>
</repositories>
<distributionManagement>
    <repository>
        <id>gitlab-maven</id>
        <url>https://git.doit.wisc.edu/api/v4/projects/PROJECT_ID/packages/maven</url>
    </repository>
    <snapshotRepository>
        <id>gitlab-maven</id>
        <url>https://git.doit.wisc.edu/api/v4/projects/PROJECT_ID/packages/maven</url>
    </snapshotRepository>
</distributionManagement>

F) Create the settings.xml file.

The settings.xml file can reside in next to the pom.xml file in root of project or at .m2 directory (in Mac OS /Users/username/.m2).

$ vi /Users/username/localmavenregistryex/mvnexample/settings.xml 

<settings>
    <servers>
        <server>
            <id>gitlab-maven</id>
            <configuration>
                <httpHeaders>
                    <property>
                        <name>Private-Token</name>
                        <value>PRIVATE_TOKEN_GOES_HERE</value>
                    </property>
                </httpHeaders>
            </configuration>
        </server>
    </servers>
</settings>


Replace PRIVATE_TOKEN_GOES_HERE with your private token created in GitLab (step A3).

G) Deploy to Gitlab Registry

To deploy to Gitlab navigate to the local root directory path. Run this command "ONLY" creates the Maven Repository on Gitlab.
In this example the settings.xml file is being read from local directory.


$ cd /Users/username/

$ docker run -it --rm --name maven-deploy \
-v "$PWD"/mvnexample:/usr/src/app \
-v "$HOME"/.m2:/root/.m2 \
-w /usr/src/app maven:3.6-jdk-8 \
mvn deploy -s settings.xml


H) Key variables for uploading and downloading files

Project Name: Maven Repository
Project ID: 9958
visibility: private
groupId=edu.wisc.sampleGroupID
artifactId=sampleArtifactID
repositoryId=gitlab-maven


I) Upload Artifacts to Gitlab-Maven
From artifacts.doit.wisc.edu download locally the files for migration to Gitlab-Maven. For this example JAR and POM files are being uploaded into Gitlab. Please notice the paths for these files below.

mvn deploy:deploy-file -s mvnexample/settings.xml -DgroupId=edu.wisc.spamkiller -DartifactId=redsand \
-Dversion=1.2.0 -Dpackaging=jar -Dfile="/Users/username/Documents/MAVEN/myuwErrorValve/myuw-error-valve-1.2.0.jar" \
-Durl=https://git.doit.wisc.edu/api/v4/projects/9893/packages/maven \
-DrepositoryId=gitlab-maven \
-DpomFile="/Users/username/Documents/MAVEN/myuwErrorValve/myuw-error-valve-1.2.0.pom"


Check the Packer Registry below to see the uploaded files.


ScreenShot2021-10-25at4.24.47PM





Keywords:GitLab Maven Registry   Doc ID:114481
Owner:John H.Group:Shared Tools
Created:2021-10-25 10:09 CDTUpdated:2022-02-10 09:39 CDT
Sites:Shared Tools
CleanURL:https://kb.wisc.edu/shared-tools/create-maven-registry
Feedback:  0   0