Aleksandar Markicevic
Aleksandar Markicevic

Reputation: 131

How to deploy private jar to gitlab Maven repository

I would like to know if it is possible to deploy a custom jar file into an existing maven repository. By custom, I mean, my own existing jar from my local machine.

So, I have a project test-project on gitlab (https://gitlab.com/group/test-project), and it is a java application that I build and deploy using gitlab pipelines. In the build stage, I am doing mvn deploy and the package test-project.jar is pushed to a gitlab maven repository (https://gitlab.com/group/test-project/-/packages).

Now, I have a dependency.jar file which is listed as a dependency in my pom.xml. I would like to push that file into the same maven repository as my test-project.jar. There is nothing about that in the documentation here. https://docs.gitlab.com/ee/user/packages/maven_repository

I tried to push it with maven-deploy-plugin in a way described here http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html

I am getting an error Failed to deploy artifacts: Could not find artifact... which is strange because I am trying to push new artifact, of course, it does not exist.

I am not sure if this is an issue with GitLab (I doubt that) or I am doing something wrong, or what I want is just not possible.


Edit

I was able to push the package but the thing is I needed to run deploy:deploy-file from the build, not from my local.

This would be the pipeline configuration


stages:
  - build

maven-repo-push:
  stage: build
  image: maven:3.6.2
  variables:
    GROUP: "com.example"
    ARTIFACT: "myproject"
    VERSION: "1.1.1"
    REPO_PROJECT_ID: "12345678" #gitlab project ID
  script:
    - mvn deploy:deploy-file
      -DgroupId=$GROUP
      -DartifactId=$ARTIFACT
      -Dversion=$VERSION
      -Dpackaging=jar
      -Dfile=$ARTIFACT-$VERSION.jar
      -DgeneratePom=false
      -DpomFile=$ARTIFACT-$VERSION.pom
      -Dfiles=$ARTIFACT-$VERSION-tests.zip
      -Dtypes=zip
      -Dclassifiers=tests
      -DrepositoryId=gitlab-maven
      -Durl=https://gitlab.com/api/v4/projects/$REPO_PROJECT_ID/packages/maven
      -s settings.xml
  when: manual

Make sure you have pushed with this configuration the files you want to push in format $ARTIFACT-$VERSION.jar and

The default settings.xml needed for authentication https://docs.gitlab.com/ee/user/packages/maven_repository/#authenticating-with-a-ci-job-token

Still, my question remains, is it possible to do this from local

Upvotes: 9

Views: 8467

Answers (1)

Alex Duzsardi
Alex Duzsardi

Reputation: 131

You can publish a library/jar from local file system as well, just have a settings.xml something like

<settings>
  <servers>
    <server>
      <id>gitlab-maven</id>
      <configuration>
        <httpHeaders>
          <property>
            <name>Private-Token</name>
            <value>PUT-YOUR-PRIVATE-TOKEN-HERE</value>
          </property>
        </httpHeaders>
      </configuration>
    </server>
  </servers>
</settings>

Or if you wan to use a group token or deploy token , make sure you update the property name above , documented here https://docs.gitlab.com/ee/user/packages/maven_repository/#authenticate-with-a-deploy-token-in-maven

And then use this to publish your jar on GitLab's package registry, keep in mind that you can only publish to project api endpoints , but you can use group endpoints to download/install artifacts locally

mvn -s settings.xml deploy:deploy-file \
-Durl=https://gitlab.com/api/v4/projects/<PUT-PROJECT-ID-HERE>/packages/maven \
-DrepositoryId=REPOSITORY-ID # has to match the settings.xml server:id \
-Dfile=your-lib.jar \
-DgroupId=your.group.id \
-DartifactId=artifact-name \
-Dversion=1.0 \
-Dpackaging=jar \
-DgeneratePom=false 

Upvotes: 12

Related Questions