Using GitHub as a Maven Repository
This exercise shows how you can use GitHub as a Maven repository — it's not the professional way to do things, but it's fun just to learn how it works!
Join the DZone community and get the full member experience.
Join For FreeIntroduction
In this short article, I'm going to explain how to use GitHub as a Maven Repository. This is a cheap alternative to using an artifactory like Nexus, JFrog, etc.
Disclaimer
This is not the recommended way to host a Maven repository. This tutorial is an exercise for learning purposes only, not the proper way of hosting a repository. You must go with Nexus, JFrog, etc. for the professional experience.
Maven Project
Let's create a simple Maven project. We will export this project as artifact so that it can be used in other projects. This artifact will be pushed to GitHub. Here is the pom.xml of the project:
<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.anupam</groupId>
<artifactId>test-repo</artifactId>
<version>1.0.0</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<distributionManagement>
<repository>
<id>internal</id>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
</project>
The most important part of the pom.xml is:
<distributionManagement>
<repository>
<id>internal</id>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
The URL can be anything valid. Now execute the following command:
mvn deploy
It will generate the artifact as shown in the below diagram:
Pushing the Artifact to GitHub
Let's create an empty GitHub repository. Let's suppose its URL is https://github.com/anupamgogoi0907/artifacts.git.
Now clone this repository to your system:
>test-repo git clone https://github.com/anupamgogoi0907/artifacts.git
Copy the artifact generated under the mvn-repo folder (in the previous step), and then push it to the repository.
It should look something like this in your GitHub account:
At this stage, we have pushed our artifact to our GitHub repository. Now let's try to make use of it.
Using the GitHub Artifact
Now let us create a new Maven project and use the artifact we pushed to GitHub in the previous step.
Add the following repository declaration in the pom.xml.
<repositories>
<repository>
<id>mvn-repo</id>
<url>https://rawgit.com/anupamgogoi0907/artifacts/master</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
And that's it. Execute the mvn compile
command and the artifact will be downloaded from the repository.
Conclusion
In this article, I have shown how easy it is to use the GitHub as your repository manager. Remember, this is just an exercise to show how it can be done, and isn't the professional way to host a repository.
Opinions expressed by DZone contributors are their own.
Comments