Import project from bitbucket to local maven repository - java

I have a maven project in bitbucket account. I need to add that the project in my local maven repository so that I can refer that project as a maven dependency in my main project's pom.xml.
That is, perform below steps
1) Clone the project
2) run maven build install This will add to local maven repository
Please let me know if there is a way to achieve the same (in similar ways to connecting to maven public repository) ?

You can achieve this by defining repositories in pom file of the project where you want to use project 1 as a dependency.
For eg.
Git clone path of Project 1: https://github.com/yogi21jan/project1
Add below code in child project:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
And finally Add this project as a dependency:
<dependency>
<groupId>group id of project 1</groupId>
<artifactId>artifcat id of project 1</artifactId>
<version>required version</version>
</dependency>

Look into jitpack.io. It can create a Maven repository for a GitHub project and also other hosters like BitBucket, GitLab, Azure, Gitee.
I have choosen a random Maven based project,
here fastconverter. It not GitHub is used, the full URL has to be provided. The created jar is published then here.

Related

How to consume a jfrog artifactory in a maven project

I created a sample project A and pushed it to my jfrog artifactory. Now, I want to use this in another maven project B.
To achieve this, I added this dependency declaration (copied from jfrog) in B's pom.xml:
<dependencies>
<dependency>
<groupId>com.testartifactory.example</groupId>
<artifactId>testartifactory</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>testartifactory</id>
<name>com.testartifactory.example</name>
<url>https://myjfrogurl/artifactory/libs-release-local/com/testartifactory/example/testartifactory/</url>
</repository>
</repositories>
But when I import this import com.testartifactory.example;, I get error that package com.testartifactory does not exist.
Even though I can see the project downloaded in repositories under .m2 folder:
Here is how my project A is structured:
I am new to this so I may be doing something wrong and would appreciate any help.
The repository URL should correspond to the repository in JFrog Artifactory. In your case: https://myjfrogurl/artifactory/libs-release-local
In the pom.xml:
<repositories>
<repository>
<id>testartifactory</id>
<name>com.testartifactory.example</name>
<url>https://myjfrogurl/artifactory/libs-release-local</url>
</repository>
</repositories>
Other options to use Artifactory for Maven builds are to use the Artifactory Maven plugin or the JFrog CLI. With these integrations, you'll get some more benefits such as build-info, Xray scanning, better performance and more.
Read more about Maven and Artifactory:
Maven Repositories in JFrog Artifactory
QuickStart Guide: Maven and Gradle
Running Maven Builds using the JFrog CLI

Maven not able to find dependency

I am trying add this dependency in my pom.xml, but maven is not able to resolve the dependency for the same.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
I was working on my other computer some weeks ago. I switched my laptop, and trying to setup this project, but it's not working anymore.
Error:
Missing artifact org.springframework:spring-test:jar:4.3.4.RELEASE
I see this this is available in the maven central.
http://search.maven.org/#artifactdetails%7Corg.springframework%7Cspring-test%7C4.3.4.RELEASE%7Cjar
Does anybody have any idea why is it happening?
Thank you #Reek for the comment.
I added spring repository url in my pom.xml and it started working.
<repository>
<id>repository.spring.release</id>
<name>Spring GA Repository</name>
<url>http://repo.spring.io/release</url>
</repository>
You do not need to add spring specific repository to use those artifacts.
Maven works as follows:
Firstly Maven tries to find artifact in the local repository.
Otherwise Maven tries to download artifacts from http://repo1.maven.org/maven2/ by default.
If you wanna build your project on another computer for the first time than there is no local repository at the moment. There are two possible reasons for Error on the second stage:
Maven central repository is unavailable from another computer: check this URL.
Mirror of Maven central is misconfigured in settings.xml like this:
<mirrors>
<mirror>
<id>central-proxy</id>
<name>Proxy of Maven Central</name>
<url>http://some/invalid/url/here</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
Detailed logs are required.

Loading Maven dependencies from GitHub [duplicate]

This question already has answers here:
Can I use a GitHub project directly in Maven?
(3 answers)
Closed 2 years ago.
How do I add a Java library from its GitHub repo (the library uses Maven as a build system) as a dependency to my Maven project? Can I do that without downloading and compiling the library?
Now you can import a Java library from a GitHub repo using JitPack.
In your pom.xml:
Add repository:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Add dependency
<dependency>
<groupId>com.github.User</groupId>
<artifactId>Repo name</artifactId>
<version>Release tag</version>
</dependency>
It works because JitPack will check out the code and build it. So you'll end up downloading the jar.
If the project doesn't have a GitHub release then its possible to use a commit id as the version.
At the moment there is no way you can do this unless the maintainer of the library provided a way to do this.
So on the title page of the library the should be an instruction containing the repository address like:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
And a dependency name:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
This means that all artifact of your project including your dependency will be searched in this repo.
You could also have a glance at pom.xml to check if there was an effort made to deploy artifacts to a remote repo. Typically the keywords are oss.sonatype.org or raw.github.com like in this case.
FYI, here is a way to provide a repo for your gihub artifact: Hosting a Maven repository on github.
Github now supports packages https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages
You can follow the steps above to deploy Jar files to github properly.
Another very nice thing about Jitpack is, it has a lookup button on the main page. And if you type the URL of your GitHub repository, it displays different commits of the source code, and you can select which commit/tag you want. The Jitpack creates pom dependencies for you.
It became dead simple.

How to make m2e (maven integration for eclipse) know where to find dependencies?

When work in STS (springsource tool suite), open pom.xml file and check dependencies in "dependencies tab", add/select maven dependency will give 0 result found. We need edit pom.xml to add dependency by hand. Is there a way to let M2E know where to check repository to search dependency?
M2e by default searches maven central repository for artifacts. It downloads an index file which has artifact details. You can configure additional repositories to be searched by specifying the same in repository section of your settings.xml file. Do note that some repositories do not have this index file.
By default , all pom.xml will automatically extend the Super POM , which is located in the maven_installation_folder/lib/maven-x.x.x-uber.jar ==> package org.apache.maven.project ==> pom-4.0.0.xml . All the configuration specified in the Super POM is inherited by the POMs you created for your projects.
If you open pom-4.0.0.xml , you will find that the maven central repository http://repo1.maven.org/maven2/ is defined here . That means if you specify a <dependency> in your pom.xml , maven will try to download this dependency in the following orders:
Maven local repository (i.e your local hard disk)
Maven central repository specifed in the Super POM (i.e http://repo1.maven.org/maven2/)
Maven remote repository (Defined in the <repository> section of your pom.xml )
Normally , I will use some maven repository search engines , such as this , to find out the <dependency> of the libraries /frameworks /tools that I want to use and then paste them in the pom.xml
If the dependency exists in the Maven central repository , everything will be fine and it will be download to your local repository . However , if some of the <dependency> (eg Hibernate) cannot be found and downloaded from the Maven central repository , you can try to visit its official site to find out its repository link and paste them in the <repository> section of your pom.xml .For example , Hibernate require to define the jboss repository in the pom.xml like this:
<repositories>
<repository>
<id>repository.jboss.org</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public-jboss</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
You can refer to this for the use of <repository>

How to add Maven dependencies from different repositories

For example, let's say I have a Maven build file that has dependencies located in two different repositories:
<repository>
<id>id1</id>
<name>id1</name>
<url>http://localhost/mavenRepo/repository1</url>
</repository>
<repository>
<id>id2</id>
<name>id2</name>
<url>http://localhost/mavenRepo/repository2</url>
</repository>
With one dependencies from the first repository and the other from the second repository. How can I do this?
Just list all the dependencies in your <dependencies> section, and Maven will contact each repository in turn (including your local repository) to find them.
A POM file has no concept of "get these dependencies from this repository and get these dependencies from that repository".
How can you do what? What is the problem? Just add the dependencies and Maven will try to resolve them from central repository and then fallback on the next declared repository if not found and so on.

Categories

Resources