I want to upload a JAR to a GitLab package registry

Viewed 2185

I have a prebuilt JAR file that I want to upload into a GitLab package registry so it can be using when a build is performed.

This is my first time playing with Maven and uploading so newbie alert here!

In my project in GitLab I do see that there is a section titled "package registry". I assume that I need to upload it there.

I found the following command which I thought might do the trick:

mvn deploy:deploy-file -DgroupId=com.acme.group -DartifactId=project-name \
-Dversion=1.2.3 -Dpackaging=jar -Dfile="test.jar" \
-Durl=https://gitlab-np-services.com/Ernie/ernie-sandbox-1/-/packages
-DrepositoryId=project-name 

I have no idea what to put in for "DrepositoryId", "DartifactId". The other field I assumed in what i put.

How do I know what to put in those fields?

I run it and get the following :

Uploading to remote-repository: https://gitlab-np.services.com/Ernie/ernie-sandbox-1/-/packages/com/acme/group/project-name/1.2.3/project-name-1.2.3.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.793 s
[INFO] Finished at: 2021-06-01T15:51:09-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact com.acme.group:project-name:jar:1.2.3 from/to remote-repository (https://gitlab-np-services.com/Ernie/ernie-sandbox-1/-/packages): transfer failed for https://gitlab-np.services.com/Ernie/ernie-sandbox-1/-/packages/com/acme/group/project-name/1.2.3/project-name-1.2.3.jar, status: 422 Unprocessable Entity -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
vanduyne-16mb:ernie-sandbox-1 vanduyer$ -DrepositoryId=project-name 
bash: -DrepositoryId=project-name: command not found
Perhaps I need to allow permissions to upload to that location in GitLab - which I have no clue how to do.

Is that even where I should upload it to?

I have tried looking at the Maven sites and other sites for help on how to do this but I can not find something explaining this for a newbie.

Thanks for any help!

1 Answers

There are a few problems that I will list below.

  • First of all, you are missing a \ at the end of the third line. So, the -DrepositoryId=project-name parameter is not passing to the mvn deploy command.

  • Next, you need to decide which level do you want to deploy the artifact? You can read more about the levels on Gitlab's official documentation (GitLab endpoint for Maven packages). These addresses lead to /packages/maven, but you are missing the /maven token in the URL. (https://gitlab-np-services.com/Ernie/ernie-sandbox-1/-/packages)

  • You don't need to pass a repositoryId if you didn't configure any server inside your settings.xml file. If you configured it, then you can use the defined Id. So, Maven can use the configuration from the settings.xml file. You can read more about the repositoryId on Maven's official documentation: repositoryId

  • The groupId and artifactId parameters are the required parameters that define how you can import the artifact from the repository. I suggest you use the same groupId and the artifactId configured inside the pom.xml file of the artifact. If you deploy a package with groupId=com.group and artifactId=artifact-id then you can import the artifact to your project like this:

     <dependencies>
         <dependency>
             <groupId>com.group</groupId>
             <artifactId>artifact-id</artifactId>
             <version>...</version>
         </dependency>
         ...
     </dependencies>
    

I suggest read more about deploying to Gitlab's Maven repository since you have to configure the authentication section as well: Maven packages in the Package Repository

Related