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?
-
1This should help: http://stackoverflow.com/q/8871056/1180621 – metrimer May 04 '15 at 21:57
-
2Does this answer your question? [Can I use a GitHub project directly in Maven?](https://stackoverflow.com/questions/8871056/can-i-use-a-github-project-directly-in-maven) – Ilya Lysenko Mar 02 '20 at 20:27
4 Answers
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.

- 26,885
- 12
- 107
- 96
-
13Small note: You must purchase Jitpack Enterprise if your company uses Github Enterprise. – Myles Baker Mar 07 '16 at 22:42
-
1what if it doesnt use Maven as its build system, like `https://github.com/mongodb/mongo-java-driver`? Maven won't accept `
com.github.mongodb `. – phil294 Nov 30 '16 at 00:29 -
1It supports other build systems like Gradle/Sbt. https://jitpack.io/#mongodb/mongo-java-driver – Andrejs Dec 09 '16 at 11:31
-
3
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.

- 1
- 1

- 16,160
- 5
- 51
- 68
-
1
-
1@tribbloid I thought most of them provide repositories, no? Just to be able to deploy automatically with Maven? – Andrey Chaschev Jul 17 '14 at 10:37
-
This is an instruction on an easy take on how to reverse-engineer the deployment provided by library supplier. raw.github.com looks obsolete now, so there must be some other host used for the artifact deployment. To find out the name, you can look inside the `pom.xml` of this library and try reaching this artifact by its "address" – Andrey Chaschev Jun 09 '17 at 17:35
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.

- 582
- 5
- 13
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.

- 3,788
- 30
- 31