Answers: <p>You might use docker builder pattern. Briefly you need to create Dockerfile.build which adds <code>pom.xml</code> and run <code>mvn dependency:resolve</code>:</p>
  
  <pre><code>FROM maven:latest
  ADD ./pom.xml /src/pom.xml
  WORKDIR /src
  RUN mvn dependency:resolve
  </code></pre>
  
  <p>Rebuild that image every time prior build. Docker will use cached image if ./pom.xml has not changed. </p>
  
  <p>Docker 17.05 introduced 'multi-stage builds' feature which implements this pattern. More information is available here: <a href="https://docs.docker.com/engine/userguide/eng-image/multistage-build/" rel="nofollow noreferrer">https://docs.docker.com/engine/userguide/eng-image/multistage-build/</a></p>
  
Answers: CC BY-SA 3.0
Answers: 2017-09-18T13:48:28.633Z
Answers: 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.
Answers: Bambara
Answers: CC BY-SA 3.0
Answers: 2017-09-18T13:53:29.893Z
Answers: 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!
Answers: Bambara
Answers: CC BY-SA 3.0
Answers: 2017-09-18T16:02:07.907Z
Answers: 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.
Answers: Alexander Matyushentsev
Answers: CC BY-SA 3.0
Answers: 2017-09-18T18:37:27.500Z
Answers: 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.
Answers: Bambara
Answers: CC BY-SA 3.0
Answers: 2017-09-18T05:00:36.677Z
Answers: 2017-09-18T05:00:36.677Z
Answers: Alexander Matyushentsev
Body: <p>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.</p>
  
  <p>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?</p>
  
Comments: CC BY-SA 3.0
Comments: 2018-04-02T05:38:45.227Z
Comments: Related question: https://stackoverflow.com/questions/47969389/how-can-i-cache-maven-dependencies-and-plugins-in-a-docker-multi-stage-build-lay
Comments: Nikolaos Georgiou
Tags: <docker><jenkins><jenkins-pipeline><maven>
Title: What is the best way to share maven repository using Docker build (dynamic) image with maven?
UserDisplayName: Bambara
ThreadText: # What is the best way to share maven repository using Docker build (dynamic) image with maven?
  
  (Asked by: username_0 on 2017-09-15)
  
  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?
  
  **Tags:** docker, jenkins, jenkins-pipeline, maven
  
  > **Comments:**
  > **username_1**: Related question: https://stackoverflow.com/questions/47969389/how-can-i-cache-maven-dependencies-and-plugins-in-a-docker-multi-stage-build-lay  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-09-18. Score: 3 | Accepted answer
  
  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/>
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_0**: 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!  
  > **username_2**: 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.  
  > **username_0**: 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.  
  
  ---
  
  