Martijn Hiemstra
Martijn Hiemstra

Reputation: 1170

Gitlab maven deploy to package registry

I am trying out gitlab and it looks very promosing. I have run into a huge problem and it seams as if nobody on the entire Internet has even a clue how to solve this.

I have added a project which is a maven project (There is only a pom.xml) and I want to deploy this to the package registry. If I do this from my home computer it is simple. I just add a deploy token to my settings.xml and do a mvn deploy.

I want to do a maven deploy to the package registry on each build so when I commit and push my code it needs to build. The thing is, how can I pass on the Deploy token to the pipeline? So in short I want to auto deploy a maven library/dependency so that other projects can use that dependency. This is how it should work however this is completly undocumented how to do it in gitlab and it almost seams as if nobody on this planet does it this way.

So in short, how can I deploy a maven project to the project/group package registry?

Upvotes: 4

Views: 10565

Answers (2)

und3rh00ds
und3rh00ds

Reputation: 11

Suppose you have a maven project (your library) which you want to deploy in gitlab so that you can use it as dependency in other maven projects, then you need to configure with the points below first in your library.

Part 1: Publishing the library in gitlab.

  1. Upload your library into gitlab first in a repository. You can also deploy the library in some other project's package registry, in either case note down the PROJECT_ID of the project where you want to deploy which can be found in settings page for that project.

  2. Add settings.xml in your project.

<settings xmlns="https://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://maven.apache.org/SETTINGS/1.1.0 https://maven.apache.org/xsd/settings-1.1.0.xsd">
          <servers>
            <server>
              <id>sample-lib</id>
              <configuration>
                <httpHeaders>
                  <property>
                    <name>Private-Token</name>
                    <value>TOKEN_VALUE</value>
                  </property>
                </httpHeaders>
              </configuration>
            </server>
          </servers>
        </settings>

Note:

  • Replace TOKEN_VALUE with your token value generated in gitlab. This can be your project token or your PAT (personal access token). Maven will use this token while authenticating with gitlab during deployment.
  • Make sure you use this same id in your pom.xml configuration.
  1. Add <dependencyManagement> and <repositories> tag in your pom.xml.

<repositories>
  <repository>
    <id>rakshit-sample-lib</id>
    <url>https://gitlab.txninfra.com/api/v4/projects/PROJECT_ID/packages/maven</url>
  </repository>
</repositories>
<distributionManagement>
  <repository>
    <id>sample-lib</id>
    <layout>default</layout>
    <url>https://gitlab.example.com/api/v4/projects/PROJECT_ID/packages/maven</url>
  </repository>
  <snapshotRepository>
    <id>sample-lib</id>
    <layout>default</layout>
    <url>https://gitlab.example.com/api/v4/projects/PROJECT_ID/packages/maven</url>
  </snapshotRepository>
</distributionManagement>

Note:

  • id should same as used in settings.xml
  • in <url> replace example with your gitlab subdomain. if you are using just gitlab then use just gitlab.com.
  • Replace your project id in PROJECT_ID (Here in this example I am deploying the library in instance level, if you want to configure at group level settings then use group id instead. You can find detailed explanation in gitlab docs.)

Now you are all set for deployment. Fire up your terminal/cmd and run the following commands.

cd <your_library_location>; mvn --settings settings.xml deploy

Note:

  • Make sure you add your token correctly or you will get authentication error (40x).
  • In case you are stuck in 4xx errors while deploying, then try capturing the request through some sniffer and check if correct http header is being sent in request.

Part 2: Using the library as dependency.

In your project's pom.xml add the following.

<dependencies>
  <dependency>
    <groupId>GROUP_ID</groupId>
    <artifactId>ARTIFACT_ID</artifactId>
    <version>VERSION_NUMBER</version>
  </dependency>
</dependencies>

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

Note:

  • Also add settings.xml with same config as before if you are accessing the library from an external application outside of your gitlab domain.
  • Replace with your group.id and artifact.id from the library.

You are good to go now. You can find detailed explanation here in gitlab docs.

Upvotes: 0

Simon Schrottner
Simon Schrottner

Reputation: 4754

Actually there is a pretty good explanation within the gitlab documentation.

I will shortly outline this process, but in the end it is just a copy from the documentation, but the link can change, or it can be moved. So people in the future will find it as this answer.

As you can see in the explanation below, you can simply provide a settings.xml for maven with the -s <file> flag. Which allows you to store the basic settings.xml within you actual project.

  1. Create a ci_settings.xml file that serves as Maven’s settings.xml file.

  2. Add the server section with the same ID you defined in your pom.xml file. For example, use gitlab-maven as the ID:

     <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>
                 <value>${env.CI_JOB_TOKEN}</value>
               </property>
             </httpHeaders>
           </configuration>
         </server>
       </servers>
     </settings>
    
  3. Make sure your pom.xml file includes the following. You can either let Maven use the CI environment variables, as shown in this example, or you can hard code your server’s hostname and project’s ID.

     <repositories>
       <repository>
         <id>gitlab-maven</id>
         <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
       </repository>
     </repositories>
     <distributionManagement>
       <repository>
         <id>gitlab-maven</id>
         <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
       </repository>
       <snapshotRepository>
         <id>gitlab-maven</id>
         <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
       </snapshotRepository>
     </distributionManagement>
    
  4. Add a deploy job to your .gitlab-ci.yml file:

     deploy:
       image: maven:3.6-jdk-11
       script:
         - 'mvn deploy -s ci_settings.xml'
    
  5. Push those files to your repository.

Again i want to highlight that is the content of the actual documentation, and i just added it here for completeness (i am not a copy cat and do not want to get recognition of other peoples work! ;) )

Upvotes: 8

Related Questions