title: How can I cache Maven dependencies and plugins in a Docker Multi Stage Build Layer?
body: <p>I want to cache Maven dependencies in a layer of the build stage of my <a href="https://docs.docker.com/engine/userguide/eng-image/multistage-build/" rel="noreferrer">Docker Multi Stage Build</a>.</p>
  
  <p>My Dockerfile looks as follows:</p>
  
  <pre><code>FROM maven:3-jdk-8 as mvnbuild
  RUN mkdir -p /opt/workspace
  WORKDIR /opt/workspace
  COPY pom.xml .
  RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
  COPY . .
  RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package
  
  FROM openjdk:8-jre-alpine
  ...
  </code></pre>
  
  <p>```</p>
  
  <p>I based this Dockerfile on the example provided in the <a href="https://blog.docker.com/2017/07/multi-stage-builds/" rel="noreferrer">Docker Multi Stage Build blog post</a> (<a href="https://github.com/dockersamples/atsea-sample-shop-app/blob/4b9a8d8d9ba5a53b0b367988978cda09f8d8f665/app/Dockerfile#L7-L12" rel="noreferrer">also available on Github</a>).</p>
  
  <p>When I run the build, instead of seeing the dependencies downloaded once by <code>dependency:resolve</code> and then re-used by <code>package</code>, I am seeing the dependencies downloaded by both steps.</p>
  
  <p>Has anyone got this working? What am I doing wrong here?</p>