Loading Maven dependencies from GitHub

JavaMavenGithubDependencies

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

Now you can import a Java library from a GitHub repo using JitPack. In your pom.xml:

  1. Add repository:

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

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

Solution 2 - Java

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: https://stackoverflow.com/questions/14013644/hosting-a-maven-repository-on-github.

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArielleView Question on Stackoverflow
Solution 1 - JavaAndrejsView Answer on Stackoverflow
Solution 2 - JavaAndrey ChaschevView Answer on Stackoverflow
Solution 3 - JavadavidView Answer on Stackoverflow
Solution 4 - JavaMeminView Answer on Stackoverflow