Using a maven repository for delivering JAVA based software in docker images

The traditional toolchain for publishing JAVA artifacts (JARs etc.) is based on Maven, its deploy plugin and a Maven artifactory like Sonatype Nexus.

On the other hand the traditional toolchain for installing software in Linux is using the package manager of the distribution like yum or apt-get.

Docker is an increasingly popular infrastructure, which aims to build, ship and run any app, anywhere. Based on Linux, and making heavy use of the well-known ways how to install software in Linux.

So it’s an interesting task how to bring both worlds and their software repositories together. This walk-through is based on Fedora and the RPM-based Linux distros.

First of all, Sonatype Nexus can be tweaked to be a RPM repo; to enable this you have to look and change the Maven repository according to this tutorial:

If you have done that and deploy a RPM to the repo, you are able to install it with yum, if the repo is known to the package-manager. You need to add the repo in /etc/yum/yum.repos.d:

name=My Repository

Now, we need to find a way how our JAVA based software deploys RPM to the repository. As an example I use the code provided here: This is a small JAVA project which builds a JAR file named wildfly-logstash.jar, which enhances Wildfly with an additional module. But it could be a WAR, which should be deployed in Tomcat or anything else. It is based on Maven and in the pom.xml there is a declaration which looks like this

            <classifier />

Here we use the rpm-maven-plugin to create a RPM named wildfly-logstash-1.0.0-1.rpm from the artifact which is build in the project; we declare that the JAR should be installed in /usr/share/java and that the RPM requires the wildfly RPM (which is in Fedora 21). To use this maven plugin you need the rpmbuild tool which actually creates the RPM file on the build machine.

yum install rpm-build

Now mvn clean package will create the RPM and mvn deploy pushes the RPM to the repo. On the target machine you can use yum install wildfly-logstash to pull it from the repository. The compile, package and deploy steps can be easily integrated into a CI tool like Jenkins.

If you want to use Docker to deliver your software, you write an Dockerfile which may start like this:

FROM fedora:21
MAINTAINER Johannes Beck
# Add my nexus repository as RPM source
ADD my.repo /etc/yum.repos.d/my.repo
# Update all existing packages
RUN yum clean all && yum -y update && yum clean all
# Install wildfly-logstash and all its dependencies
RUN yum -y install wildfly-logstash && yum clean all

And pushes the image to the docker repository. Again this build step can be integrated into the CI engine.

This is one way of many, many alternatives you have. The nice thing about it is, that you always use the well-proven tools of each world.

Tagged : , ,

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.