What is the best way to share maven repository using Docker build (dynamic) image with maven?
You might use docker builder pattern. Briefly you need to create Dockerfile.build which adds pom.xml
and run mvn dependency:resolve
:
FROM maven:latest
ADD ./pom.xml /src/pom.xml
WORKDIR /src
RUN mvn dependency:resolve
Rebuild that image every time prior build. Docker will use cached image if ./pom.xml has not changed.
Docker 17.05 introduced 'multi-stage builds' feature which implements this pattern. More information is available here: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
Related videos on Youtube
Bambara
Updated on September 18, 2022Comments
-
Bambara over 1 year
We have created a Docker build container to be run by Jenkins for Java based projects. We are using Maven for building our software. The problem we are facing is that, the builds are taking way to long to finish and the main reason for that is that maven is re-downloading all the dependencies for every single build, maven is run from inside the build container and not from the pipeline step.
We thought about using a shared folder but we have dynamic build agents so no shared folder. Is there anyone who is using the same approach and is there a better approach to this?
-
Admin about 6 yearsRelated question: stackoverflow.com/questions/47969389/…
-
-
Bambara over 6 yearsThanks for your answer, but that does not solve my build time issue, if the dependencies has to be downloaded again before each build, I am at the same point, but I will keep this in mind if I cannot come up with anything different.
-
Bambara over 6 yearsAnother aspect to that, my build container is generic and not specific to each project, what you are describing here will make me create a specific build container for each project since their pom files are different, but this is a very interesting concept!
-
Alexander Matyushentsev over 6 yearsDocker won't run
mvn dependency:resolve
second time ifpom.xml
has not changed since last run. So 'builder' image with installed dependencies will be created instantly for subsequent builds. You are right about downsides: you will not be able to use same image for different builds; approach might not work if you have parallel builds which are trying to create 'builder' image with the same name. -
Bambara over 6 yearsThat's good to know, I keep that in mind for future projects, I may change the current approach if build time is becoming a bigger problem.