Add GitLab private repository as Maven dependency - java

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?).

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.

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:
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.

To use your private repository as maven dependency go to your package Registry and click on the package(com/example/demo)
you will see the instruction to add the dependency.
You need to add tag with correct dependency and and tag.
if you will mention private repository url in both and , it will pull and push the artefact from the private repository.
If you want to use the private repository to only download the dependency and push your artifact to the current project registry, then you need to add the and tag of the private repository and tag will the current project one.
Scenario 1:-
<dependency>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</snapshotRepository>
</distributionManagement>
2nd scenario:-
<dependency>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/projects/36104875/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<snapshotRepository>
<id>gitlab-maven</id>
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
</snapshotRepository>
<repository>
<id>gitlab-maven</id>
<url>${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven</url>
</repository>
</distributionManagement>

Related

Intellij Maven [Cannot resolve dependency]

I set up Nexus on my server (api.lielamar.com) and uploaded my first project called PacketManager.
You can view it here: https://api.lielamar.com/#browse/browse:maven-public
For some reason, when I try to import it through pom.xml it just doesn't work. I am fairly new to Maven and extremely new to Nexus so it might be a newbie question.
I tried using google and couldn't find a direct answer that worked for me.
There's my code:
<repositories>
<repository>
<id>lielamar-repo</id>
<url>https://api.lielamar.com/service/rest/repository/browse/maven-public/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.lielamar.packetmanager</groupId>
<artifactId>PacketManager</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
I am also using the spigot repository which is working just fine.
This is what I see on the IDE
Thank you for your time!
Edit:
One of the problems were that I had my dependency calling a different repo when running mvn install or trying to compile through intellij.
After adding the following code to setting.xml, I started getting a different error:
<mirrors>
<mirror>
<id>lielamar-mirror</id>
<name>lielamar mirror</name>
<url>https://api.lielamar.com/service/rest/repository/browse/maven-public/</url>
<mirrorOf>lielamar-api</mirrorOf>
</mirror>
</mirrors>
The error I was getting after adding the above code was:
[ERROR] Failed to execute goal on project TestPlugin:
Could not resolve dependencies for project com.lielamar.testplugin:TestPlugin:jar:1.0:
Failure to find com.lielamar.packetmanager:PacketManager:jar:1.0 in
https://api.lielamar.com/service/rest/repository/browse/maven-public/ was cached in the local repository,
resolution will not be reattempted until the update interval of lielamar-mirror has elapsed or updates are forced -> [Help 1]`
I run the command mvn dependency:purge-local-repository from the cmd and it was built successfully, however, intellij still gives me an error: https://prnt.sc/unxppz
Your JAR files in the Nexus end with ..jar, so you probably made a mistake when uploading them (two dots instead of one).
You need to add in your pom.xml
<distributionManagement>
<snapshotRepository>
<repository>
<id>you-repository-name</id>
<url>http://you-repository-url</url>
</repository>
<snapshotRepository>
</distributionManagement>
It looks ok to me. Have you checked the docs? Perhaps you need to set up Maven first. To make sure it is a Maven issue, also try running your Maven build from outside of Intellij -- just a common practice, as I doubt it is the case that Intellij has anything to do with it.
Replace your repository url by https://api.lielamar.com/repository/maven-releases/
The one you have points to a group, if you check on nexus.
The group one might need to be referenced in a different way if they can be referenced at all.
or you might need something like this https://docs.gitlab.com/ee/user/packages/maven_repository/#group-level-maven-endpoint
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.com/api/v4/groups/GROUP_ID/-/packages/maven</url>
</repository>
</repositories>
<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>

mvn build keeps checking nexus repos not matter what I put in pom file

I am trying to access some jar files by including dependencies in the pom. The problem is I get error not found in my corporate nexus repo. How do I tell Maven not to look only in http://sl-quality.mycompany.com/nexus/content/groups/public/ for the dependencies? And where earth is the url defined?
Could not resolve dependencies for project com.mycompany.myproj:mycompany-service:jar:1.0-
SNAPSHOT: Could not find artifact io.confluent.ksql:ksqldb-api-client:jar:0.11.0 in nexus
(http://sl-quality.mycompany.com/nexus/content/groups/public/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myapp</groupId>
<artifactId>com-mycompany-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<ksqldb.version>0.11.0</ksqldb.version>
<!-- Maven properties for compilation -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<!-- jhipster-needle-maven-repository -->
<repository>
<id>ksqlDB</id>
<name>ksqlDB</name>
<url>https://ksqldb-maven.s3.amazonaws.com/maven/</url>
</repository>
<repository>
<id>confluent</id>
<name>Confluent</name>
<url>https://jenkins-confluent-packages-beta-maven.s3.amazonaws.com/6.1.0-beta200715032424/1/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.confluent.ksql</groupId>
<artifactId>ksqldb-api-client</artifactId>
<version>${ksqldb.version}</version>
</dependency>
</dependencies>
I have no idea why it always goes to nexus as this is no where mentioned in my pom file. I could install in my local repo but not sure what app jar files need to be pulled for it to work as two repos needed to be added.
I would like to know exactly what jar files i can extract to put in my local repo. Thanks
Repository is generally configured in your settings file located in C:/Users/Youruser/.m2/settings.xml on windows
Check the mirror and corresponding repository tags for example - your settings.xml will probably look like this
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://sl-quality.mycompany.com/nexus/content/groups/public/</url>
</mirror>
Change this url to point to another repo, perhaps an external non corporate one such as http://repo.maven.apache.org/maven2
EDIT:
I think this is what you want
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*,!ksqlDB</mirrorOf>
<url>http://sl-quality.mycompany.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<!-- Add repository for your other repo to filter on above -->
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>ksqlDB</id>
<url>https://ksqldb-maven.s3.amazonaws.com/maven/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</profile>
<!-- add at bottom inside </settings> -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
Add more repos as needed and filter above where necessary. I believe (not tested) that you can apply the same exclusions to mirrors instead of repositories personally I prefer using repository tags and having profiles setup.

How to set deployment repository in pom.xml

everybody!
When I deploy my artefact, I run
clean deploy -DaltDeploymentRepository=releases::default::http://nexus.******.com/nexus/content/repositories/releases
What should I put into pom.xml to be able to run this command from Idea without creating custom command on every maschine?
From: https://maven.apache.org/plugins/maven-deploy-plugin/usage.html
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>MyCo Internal Repository</name>
<url>Host to Company Repository</url>
</repository>
</distributionManagement>
Here you can find description of parameters you pass:
http://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html
Authentication you should store in your settings.xml file:
<server>
<id>internal.repo</id>
<username>maven</username>
<password>foobar</password>
</server>

Add mysql connector to a maven / nexus build in eclipse

I'm trying to connect to a mysql database in java and so I have to add mysql-connector-java:jar to my eclipse project. The integration with maven is not working though.
Here is what I have in my settings.xml:
<profile>
<id>default</id>
<properties>
<mvn.path>.../maven-3.0.4/bin/mvn.bat</mvn.path>
<javac.5>...bin/javac.exe</javac.5>
<javac.6>..../javac.exe</javac.6>
</properties>
<repositories>
<repository>
<id>central-repository</id>
<name>OSS central Maven Release Repository</name>
<url>https://oss.sonatype.org/content/repositories/releases</url>
</repository>
<repository>
<id>public-repository-main</id>
<name>Central Repository</name>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>public-repository</id>
<name>OSS Maven Release Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
</repository>
</repositories>
</profile>
<interactiveMode>true</interactiveMode>
<!-- offline
| Determines whether maven should attempt to connect to the network when executing
| a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false <offline>false</offline> -->
<offline>false</offline>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://xxxx:8083/nexus/content/groups/public</url>
</mirror>
And here is the error I get when I try to a mvn clean install
Failure to find mysql:mysql-connector-java:jar:5.0.5 in
http://xxxx:8083/nexus/content/groups/public
How can I force nexus to download the artifact from my public repositories to nexus? Do I have to add it manually?
You need to tell maven to use your maven repository. To do this, modify $HOME/.m2/settings.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://xxxx:8083/nexus/content/groups/public</url>
</mirror>
</mirrors>
<proxies></proxies>
<servers></servers>
<pluginGroups></pluginGroups>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
The nexus profile is configured to download from the central repository with a bogus URL of http://central.
This URL is overridden by the mirror setting in the same settings.xml file to point to the URL of your single Nexus group. The nexus group is then listed as an active profile in the activeProfiles element.
Suppose you have repository Central that has repository path http://xxxx:8083/nexus/content/repositories/central and also point Remote Storage Location to http://repo1.maven.org/maven2/ you can change http://central to http://xxxx:8083/nexus/content/repositories/central or http://repo1.maven.org/maven2/.
in your pom.xml, use below dependency to getting mysql-connector.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
See also: Getting started with Nexus maven repository manager
Hope this help.
Is there a chance that you have included two different versions for mysql-connector-java?
I used to have an error like that, it turned out I had accidentally included another version, so this specific version was not accessed (it was a transitive dependency, which for peculiar reasons I could not exclude). I moved the dependency in the beginning of all other dependencies (in your case it might be in the end),and it worked. Theoretically having two versions of same dependency should not throw an error.
Here are the available versions of MySQL connector
http://mvnrepository.com/artifact/mysql/mysql-connector-java , click on any version, it will give your the dependency.

Archetype Selection Using m2eclipse

I would like to create a new Maven project in Eclipse using an archetype using the New Maven Project wizard. At the "Select an Archetype" step, I have three catalogs to choose from: Nexus Indexer, Internal and Default Local. I don't understand where the content for these catalogs should be coming from. If I click on the "Configure" button, they are all greyed out and I can't modify them.
Only the Internal catalog has any archetypes listed in it. Where are these archetypes coming from? It's not from my local Maven repository because the group/artifacts are not in it (and archetypes in the repo are not listed).
Why is the Nexus Indexer list empty? I've read some posts that the Nexus index needs to be updated, but not how to do this. Is this different than the repository indexes (which are scheduled to be updated daily).
As you can see, I'm a little confused about the whole catalog business and how Maven, m2eclipse and Nexus interact. Any clarification is most welcome!
My setup:
Eclipse: Helios Service Release 2 (Build id: 20110218-0911)
Apache Maven 3.0.3
m2eclipse: 0.12.1.20110112-1712 (set up to use external Maven install)
Sonatype Nexus™ Open Source Edition, Version: 1.9.0.2
My local Maven settings.xml looks like this:
<settings>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://myserver:8080/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
<servers>
<server>
<id>my-snapshots</id>
<username>user</username>
<password>password</password>
</server>
<server>
<id>my-releases</id>
<username>user</username>
<password>password</password>
</server>
</servers>
</settings>
The default archetype catalog can contain new archetypes. It's a manual process, you've to add them to a custom .xml file within your ~/.m2 directory.
For more information : http://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-catalog.html
And for reference, here's a template archetype-catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog>
<archetypes>
<archetype>
<groupId>com.spedge</groupId>
<artifactId>archetype-application-custom-filter</artifactId>
<version>1.0-SNAPSHOT</version>
</archetype>
</archetypes>
</archetype-catalog>
Additionally, here's a good link on SO about Archetypes : What are the URLs of all the Maven Archetype catalogs that you know about?

Categories