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?


1 Answer 1


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
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/

  • Thanks 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
    Sep 18, 2017 at 13:48
  • Another 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!
    – Bambara
    Sep 18, 2017 at 13:53
  • Docker won't run mvn dependency:resolve second time if pom.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. Sep 18, 2017 at 16:02
  • That'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.
    – Bambara
    Sep 18, 2017 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.