How to deploy private jar to gitlab Maven repository - java

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

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

Related

Multi Module Maven Project and Docker: Cannot find artifact?

I have the following multi-stage dockerfile that attempts to copy each module of my multi-module java maven project and build my ear file using mvn clean install.
dockerfile:
# Copy files from local to maven image and build ear
FROM maven:3.5-jdk-8 AS build
COPY module1 /usr/src/app/src
COPY module2 /usr/src/app/src
COPY module3 /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean install
# Create server image + rest of docker file (working ok)
The error that I am getting is as follows:
Step 8/20 : RUN mvn -f /usr/src/app/pom.xml clean install
---> Running in cf9d8c1ef9ed
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.company.web:api:21.01-SNAPSHOT:
Could not find artifact com.company.parent:jee6:pom:1.0.1-SNAPSHOT and
'parent.relativePath' points at wrong local POM # line 8, column 10
#
[ERROR] The build could not read 1 project -> [Help 1]
The section in the pom.xml that corresponds to this error:
<parent>
<groupId>com.company.parent</groupId>
<artifactId>jee6</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
I assume this issue because when trying to run the command in the maven docker image it cannot see my local .m2 folder? And the dependency that it is looking for is a private dependency on my local machine.
Would also copying my maven settings.xml help?
How can I resolve this? I do not want someone to have to have Maven installed on their machine in order to run this dockerfile.
Dependency com.company.parent:jee6:pom:1.0.1-SNAPSHOT seems to be private, your Maven command inside Docker build needs to be able to either download it from private repository or have it readily accessible.
I assume this issue because when trying to run the command in the maven docker image it cannot see my local .m2 folder?
Yes, it then cannot see your settings.xml with private repository config, or local dependency if it's already available locally.
Would also copying my maven settings.xml help?
It's better not to: your settings.xml (and eventual secrets within) may be available to anyone using your image later. Using a secret mount with BuildKit would be a better solution (see below)
You have multiple solutions:
Mount settings.xml as secret during build
This solution assumes you have a settings.xml configured with proper credentials to access private registry.
Use Docker BuildKit with --mount=secret to load settings.xml as secret with a Dockerfile such as:
# syntax=docker/dockerfile:1.2
# Required comment at top of Dockerfile for using BuildKit
FROM maven:3.5-jdk-8 AS build
COPY module1 /usr/src/app/src
COPY module2 /usr/src/app/src
COPY module3 /usr/src/app/src
COPY pom.xml /usr/src/app
# Use your secret settings.xml
RUN --mount=type=secret,id=mvnsettings,target=/root/.m2/settings.xml \
mvn -f /usr/src/app/pom.xml clean install
And build command such as:
DOCKER_BUILDKIT=1 docker build --secret id=mvnsettings,src=$HOME/.m2/settings.xml .
Maven should then be able to download parent dependency during build.
Note: this is NOT COPYing the settings.xml in image, as the secret settings.xml will only be made available for the specified build step and won't be persisted in final image.
Copy com.company.parent:jee6 pom.xml during build
This solution is less practical and may not solve problem entirely:
It would require to have com.company.parent:jee6:pom:1.0.1-SNAPSHOT pom.xml file available in build context
Your parent pom.xml may refer to other private dependencies. You would have to include them the same way.
... But it still may be worth a try.
You can do something like:
FROM maven:3.5-jdk-8 AS build
# Copy and install parent pom
COPY parent-pom.xml /tmp/parent/pom.xml
RUN mvn -f /tmp/parent/pom.xml clean install
COPY module1 /usr/src/app/src
COPY module2 /usr/src/app/src
COPY module3 /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean install
There are 2 different parts to the error: Cannot find the parent, and the parent.relativePath element is wrong. I think the 2nd part might be causing the 1st.
Since your parent element doesn't specify a relativePath element, the default is the module parent path (aka ..). Your modules are not in a child of the parent folder (/usr/src/app) but rather in the (/usr/src/app/src subfolder).
Try changing your copy commands to :
COPY module1 /usr/src/app
COPY module2 /usr/src/app
COPY module3 /usr/src/app
COPY pom.xml /usr/src/app
You should then see:
/usr/src/app
/usr/src/app/module1
/usr/src/app/module2
/usr/src/app/module3

Deploy External Library on Nexus using Maven

I'm using Maven & Nexus repository.
The setup looks good, my project deploy on Nexus properly, but I would like to add my External library loaded by Maven as well on Nexus and I don't find the way to do it.
Do you know how to do it ?
Many thanks,
To deploy an external library you can use the Maven goal deploy:deploy-file.
I took the following example from the Nexus 3 support
On the command line this could look like this:
mvn deploy:deploy-file -DgroupId=com.somecompany -DartifactId=project -Dversion=1.0.0 -DgeneratePom=true -Dpackaging=jar -DrepositoryId=nexus -Durl=http://localhost:8081/repository/maven-releases -Dfile=target/project-1.0.0.jar
The repositoryId is a server defined in your settings.xml.
<servers>
...
<server>
<id>nexus</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
Hint: As this is a bit cumbersome. Check if your library is available on a public Maven repository. If it is available just add that repository to Nexus as Proxy Maven Repository.

Manually downloading and installing maven plugins

I am on proxy network and proxy is preventing maven to connect to central repo
Is there any way to download the maven plugins manually and installing the plugins in local .m2 repo.
Thanks
If you have the jar, you can use :
mvn install:install-file -DgroupId=<your_group_name> \
-DartifactId=<your_artifact_name> \
-Dversion=<version> \
-Dfile=<path_to_your_jar_file> \
-Dpackaging=jar \
-DgeneratePom=true
An example of the same:
mvn install:install-file \
-DgroupId="org.apache.maven.plugins" \
-DartifactId="maven-site-plugin" \
-Dversion="3.7.1" \
-Dfile="D:\Jars\maven-site-plugin-3.7.1.jar" \
-Dpackaging=jar \
-DgeneratePom=true
if you don't have the (plug-in) jar, and it's not a private artefact, you can find the jar and the information in repository website (for example http://mvnrepository.com/ ) or download from your private repository like Nexus or Azure Feeds.
Have a look here if you want to configure a proxy for maven. That way you do not need to download the dependencies in any obscure and unreliable way: Howto configure a proxy for Maven
The simplest way to do this is to use any machine that can connect to central to download all repo you need then copy ./m2 manually to your machine with usage of offline flag every build
if your solution is scalable you will need to install nexus or any maven repository on machine can goes outside and you configure maven to see repo machine. and this is the model solution to that case
To install a jar you can use install:install-file to install any kind of jar. But a maven plugin comes with dependencies so you will have to install them. My suggestion would be to copy a .m2 repo from a machine with access to the internet (for maven).

How to link local repository in the maven build process

How to point to a local repository in the maven build process. In that case I want to link directory with custom jars with the compilation and packaging.
Tried following "mvn compile -Dmaven.repo.local=E:\Test\lib" but it doesn't use located jars for the compilation.
Is there way to include repository through maven pom.xml in the way we inject dependencies?
Just install the custom jars into your local repository (usually located under C:..User.m2\repository) via maven-install-plugin:
mvn install:install-file -Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-Dsources=src.jar] \
[-Djavadoc=apidocs.jar] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=sources] \
[-DgeneratePom=true] \
[-DcreateChecksum=true]
But i would suggest to install a repository manager (like Archiva, Artifactory, Nexus) and install those artifacts there. So you need to do the manual installation work only once and not on every developing machine.
The best way to configure several repositories is via the settings.xml file and not in the pom.xml file.
Exactly pom.xml is for that purpose to maintain dependencies.. you can get more on here
What do you mean by "Local Repository"?
If you mean a local Nexus / Artifactory you can use the Maven sections in your settings.xml
http://maven.apache.org/guides/mini/guide-mirror-settings.html
If you mean the local repository cache (the .m2 dir) that should just work out-of-the-box.

Spring and Maven plugin installation issue; mvn; spring

i've a maven based project and i'm trying to import it to Spring source tool suite. I am getting the following error.
Description Resource Path Location Type
Could not calculate build plan: Missing:
----------
1) org.apache.maven.plugins:maven-resources-plugin:maven-plugin:2.4.2
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.apache.maven.plugins -DartifactId=maven-resources-plugin -Dversion=2.4.2 -Dpackaging=maven-plugin -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=org.apache.maven.plugins -DartifactId=maven-resources-plugin -Dversion=2.4.2 -Dpackaging=maven-plugin -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
----------
1 required artifact is missing.
for artifact:
org.apache.maven.plugins:maven-resources-plugin:maven-plugin:2.4.2
from the specified remote repositories:
central (http://repo1.maven.org/maven2, releases=true, snapshots=false)
Gamex Unknown Maven Problem
It looks like it's asking me to install the maven resourource plugin 2.4.2 but I'm not sure what is the file to download. I tried downloading maven-plugin-plugin-2.4.2-source.jar and feeding it to the command; but it didn't work.
I then went to the maven repository in http://svn.apache.org/viewvc/maven/plugins/tags/maven-resources-plugin-2.4.2/ and downloaded the pof.xml; this also is not working.
If you have seen this problem before pls help me understand what is missing / or what the error message is.
Thanks,
Download the maven resourource plugin 2.4.2[maven-resources-plugin-2.4.2.jar] from http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/maven/plugins/maven-resources-plugin/2.4.2/ and do an
mvn install:install-file -DgroupId=org.apache.maven.plugins -DartifactId=maven-resources-plugin -Dversion=2.4.2 -Dpackaging=maven-plugin -Dfile=/path/to/file
followed by clean eclipse :eclipse , clean the project in Eclipse and refresh and try . It should work .
You may be behind firewall. You could try to set an HTTP proxy to bypass the firewall.
In Windows, this file is located at: C:\Documents and Settings\<windows Login>\.m2\settings.xml
You need to provide valid parameters to the <proxy> based on how the network is setup in your organization.
<settings
...
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>put-ur-proxy-servername</host>
<port>80</port>
</proxy>
...
</settings>

Categories