Jar dependency caching for Maven-based Docker builds

I am creating a Docker image from this Docker file:

FROM maven:3.3.3-jdk-8 MAINTAINER Mickael BARON ADD pom.xml /work/pom.xml WORKDIR /work RUN mvn dependency:go-offline --fail-never ADD ["src", "/work/src"] RUN ["mvn", "package"] 

With this Dockerfile, I force download dependencies before packing my Java project. This way, I do not need to reload the dependencies every time I change the file from the src directory.

But there is a problem, and this problem depends on the version of Maven (the base image). In fact, the dependencies are loaded, but they are not stored in the ~ / .m2 directory of the container. He is empty. Thus, when I change some source file, all dependencies are reloaded.

However, I noticed that if I change the version of Maven from the base image (e.g. FROM maven:3.2.5-jdk-8 ), it will work.

Very strange, isn't it?

+7
maven docker dependencies dockerfile
source share
2 answers

There is a new instruction in this thread: https://github.com/carlossg/docker-maven#packaging-a-local-repository-with-the-image

The $ MAVEN_CONFIG parameter (to / root / .m2 by default) is configured as one, so everything that is copied to the Dockerfile during build is lost. To do this, the dir / usr / share / maven / ref / directory is created, and something in it will be copied when the container starts in $ MAVEN_CONFIG.

To create a pre-packaged repository, create pom.xml with the necessary dependencies and use this in your Docker./usr/share/maven/ref/settings-docker.xml file - this is the settings file that modifies the local repository in / usr / share / maven / ref / repository, but you can use your own settings file if it uses / usr / share / maven / ref / repository as a local repo.

+2
source share

I am afraid of this because of this VOLUME command they added:

https://github.com/carlossg/docker-maven/blame/8ab542b907e69c5269942bcc0915d8dffcc7e9fa/jdk-8/Dockerfile#L11

It makes /root/.m2 volume, and therefore any changes to this folder made by the assembly steps are not included in the following assembly containers.

+1
source share

All Articles