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: http://books.sonatype.com/nexus-book/reference/yum-configuration.html.

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:


[my-repo]
name=My Repository
baseurl=http://nexus.example.com/nexus/content/repositories/my-repo
enabled=1
gpgcheck=0

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: https://github.com/kifj/wildfly-logstash. 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


<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>rpm-maven-plugin</artifactId>
  <version>2.1-alpha-4</version>
  <executions>
    <execution>
      <id>build-rpm</id>
      <goals>
        <goal>attached-rpm</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <group>Applications/Internet</group>
    <mappings>
      <mapping>
        <directory>/usr/share/java/x1</directory>
        <directoryIncluded>false</directoryIncluded>
        <artifact>
          <classifiers>
            <classifier />
          </classifiers>
        </artifact>
      </mapping>
      ...
    </mappings>
    <requires>
      <require>wildfly</require>
     </requires>
  </configuration>
</plugin>

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