Loading Maven dependencies from GitHub [duplicate] - java

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.

Related

Import project from bitbucket to local maven repository

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.

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.

Download JFXtras 8.0-r5 or 8.0-r6

I am trying to download the latest build of JFXtras (i want the .jar file) but i can't find it.
For 8.0-r6 i can see some .jar.soc .jar.asc what are those files? https://oss.sonatype.org/content/repositories/snapshots/org/jfxtras/jfxtras-all/
For 8.0-r5 a jar exists but it is empty it has only Meta-Inf http://central.maven.org/maven2/org/jfxtras/jfxtras-all/
How to download the jar or build it?And what are those other files there?I haven't seen them again that's why i am asking..
Problem Converting Project to Maven:
The .jar.asc files contain a PGP signature.
To make maven download the jar from maven central, add the dependency to pom.xml, e.g.
<dependencies>
<dependency>
<groupId>org.jfxtras</groupId>
<artifactId>jfxtras-all</artifactId>
<version>8.0-r5</version>
</dependency>
</dependencies>
Or grab the jars manually from http://central.maven.org/maven2/org/jfxtras/ and add them to your project.
If you want to use the bleeding edge snapshot (currently 8.0r6-SNAPSHOT), you'll need to add the sonatype repository to your pom like
<repositories>
<repository>
<id>snapshots-repo</id>
<name>Sonatype Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
And add the current snapshot to the dependencies, e.g.
<dependencies>
<dependency>
<groupId>org.jfxtras</groupId>
<artifactId>jfxtras-all</artifactId>
<version>8.0-r6-SNAPSHOT</version>
</dependency>
</dependencies>
But be aware that they're development versions (unstable), and their availability in that location can be temporary.
To download the -SNAPSHOT jars manually, you go to the sonata snapshot repository, choose the jfxtras package that you want to download, and scroll down to find the jar. They're simply not keeping all the older snapshots.

Import and use a class from a jar using Maven

I am trying to implement hoptoad in my existing maven project. I ve given the following lines in pom.xml as suggested in http://code.google.com/p/hoptoad/
<project>
<repositories>
<repository>
<id>hoptoad-repository</id>
<name>Hoptoad Repository</name>
<url>http://hoptoad.googlecode.com/svn/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>code.lucamarrocco</groupId>
<artifactId>hoptoad-notifier</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
</project>
When i try to use the HoptoadNotice in my catch block, i m getting ClassNotFoundException. I am using Eclipse IDE. I am not able to figure out, that the problem is because of inclusion of this code(in pom) or in the IDE. I can understand that the inclusion of these lines alone do not help the code to recognize the jar. So, i tried installing the jar in maven repository. But still it did not help.
Note:- I have not just blindly copied as shown above, directly into the pom.xml. I have included the lines of code wrt <dependencies> and <repositories> blocks.
The maven configuration looks correct.
Make sure that the hoptoad-notifier-1.8.jar is correct deployed.
For an standalone app, this means for example specified in the argument list or the manifiest.
For an web app, it means the jar is copied (by maven, not by you) in the libs folder.

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>

Categories

Resources