Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add GitLab private repository as Maven dependency

I have a private repository in GitLab (it's in a group, and I have a Developer role) which I want to add as a dependency for my Maven project.

I've been looking for a while and all I found was that I have to add the following to my pom.xml:

<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.com/api/v4/projects/.../packages/maven</url>
  </repository>
</repositories>

<distributionManagement>
  <repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.com/api/v4/projects/.../packages/maven</url>
  </repository>
  <snapshotRepository>
    <id>gitlab-maven</id>
    <url>https://gitlab.com/api/v4/projects/.../packages/maven</url>
  </snapshotRepository>
</distributionManagement>

However, I have no idea how to add the dependency itself (with the groupId and stuff) and I'm not sure how to do authentication to allow Maven to download the project. I've seen something about Personal Access Tokens in GitLab, but I'm not sure how to set that up (I assume I'd only need read-only access?).

like image 436
Priv Avatar asked Sep 16 '18 19:09

Priv


2 Answers

First of all you should go to your projects "Packages and Registries" it is on the left menu of your Gitlab user interface and find there package registry button(if you can't see it just ask your system administrator) and just leave that tab open and wait for future instructions. Be aware that if you work in a company that has Gitlab domain, everywhere that I write "https://gitlab.com", you should write your company's gitlab domain. After that you should generate your "Private-Token" by going to "https://gitlab.com/profile" -> "access tokens" and select api check box and give a name to your token for example "test token" and then press generate. After that in your java project create file "settings.xml" near your pom.xml and paste there following piece of code that is below and write there your token that you just generated. This settings.xml is required both for uploading and installing artifact.

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

After that go to Gitlab's user inreface and copy your project's id from there. See screenshot:project id After that paste following code that is below in your pom.xml. This must be done in the project that should be uploaded in Gitlab's "package registry"

<distributionManagement>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/project_id/packages/maven</url>
    </repository>
    <snapshotRepository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/project_id/packages/maven</url>
    </snapshotRepository>
</distributionManagement>

For uploading artifact open terminal in your ide and paste following command:

mvn deploy -s settings.xml

After that go to "package registry" of your project in Gitlab user interface and see there uploaded artifact.

For installing the settings.xml also needed, and also paste following piece of code that is below in your pom.xml

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://gitlab.com/api/v4/projects/project_id/packages/maven</url>
    </repository>
</repositories>

And in your terminal call: mvn install -s settings.xml or mvn dependency:get -Dartifact={groupId}:{artifactId}:{version} If there is an error while installing, don't worry, got to your local computer's .m2 folder, find that folder with containing new created artifact(jar), delete it and then go and call the same terminal command once again.

like image 92
Vahan Yeghyan Avatar answered Oct 22 '22 12:10

Vahan Yeghyan


First, a few pre-requisites. Since you mentioned you're using a private repository, you'll need to have at least GitLab Silver (hosted on gitlab.com) or GitLab Premium (self-hosted) in order to use the GitLab Maven Packages repository. Also, if you're self-hosted, you'll need to be on GitLab 11.3 or later (should be out this time next week), and have packages_enabled set to true (see Enabling the Packages repository).

For private projects, you'll need a Personal Access Token. The token should have the api scope for Maven to upload artifacts to GitLab. Once you have the token, you configure your settings.xml like this.

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

In pom.xml, where you have the ellipses, you need to fill in your project ID. To find the ID, just visit the front page of your project on GitLab. It's shown near the top of the pages, just after the name and description of your project. For example, take a look at the mvn-example sample project. Its project ID is 8377576. That goes in the URL.

<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.com/api/v4/projects/8377576/packages/maven</url>
  </repository>
</repositories>

The mvn-example project's pom.xml file shows a completed example.

With all that set up, you should be able to upload artifacts with mvn deploy.

like image 23
King Chung Huang Avatar answered Oct 22 '22 13:10

King Chung Huang