Dockerfile – Copy Specific extension files recursively

module Java Project which looks something like this

root
  - module1
      - src
      - target
      - pom.xml
  - module2
      - src
      - target
      - pom.xml
  - module3
      - src
      - target
      - pom.xml
  pom.xml (parent POM)

I want to write a DockerFile for this kind of multi-module project to help in CI/CD,
I want to make use of multi-stage docker builds so that I can re-use the docker cache (no dependency download on every build is the goal)

As and example, normally for 1 Pom in project Dockerfile looks like this (which I have in one of the other projects)

    FROM maven:3.6.3-ibmjava-alpine as dependency
    COPY pom.xml /home/dummy
    RUN mvn -f /home/dummy/pom.xml verify

So till the time pom.xml doesn’t get modified the docker build doesn’t download all the dependencies, but in my current multi-module setup there are child pom’s on which this parent pom is dependent, so what I actually want is:

  1. To Be able to copy all the POM files using COPY command ,with directory structure intact (not through RUN command, since that doesn’t help in caching)

I tried the below config, it copies all the files correctly but the command COPY . . causes the docker to download the dependency every time I build the image.

FROM maven:3.6.3-ibmjava-alpine as dependency
RUN mkdir /home/dummy
COPY . .
RUN find . -name 'pom.xml' -exec cp --parents {} /home/dummy ;

As last resort I am thinking of adding all the pom references statically in DockerFile, but that will be a bit cumbersome when new modules get added.

Looking for some better approach.

Source: Docker Questions