Multi Module Maven Project and Docker: Cannot find artifact?

Dependency com.company.parent:jee6:pom:1.0.1-SNAPSHOT seems to be private, your Maven command inside Docker build needs to be able to either download it from private repository or have it readily accessible.

I assume this issue because when trying to run the command in the maven docker image it cannot see my local .m2 folder?

Yes, it then cannot see your settings.xml with private repository config, or local dependency if it's already available locally.

Would also copying my maven settings.xml help?

It's better not to: your settings.xml (and eventual secrets within) may be available to anyone using your image later. Using a secret mount with BuildKit would be a better solution (see below)


You have multiple solutions:

Mount settings.xml as secret during build

This solution assumes you have a settings.xml configured with proper credentials to access private registry.

Use Docker BuildKit with --mount=secret to load settings.xml as secret with a Dockerfile such as:

# syntax=docker/dockerfile:1.2
# Required comment at top of Dockerfile for using BuildKit

FROM maven:3.5-jdk-8 AS build  

COPY module1 /usr/src/app/src  
COPY module2 /usr/src/app/src  
COPY module3 /usr/src/app/src  
COPY pom.xml /usr/src/app  

# Use your secret settings.xml
RUN --mount=type=secret,id=mvnsettings,target=/root/.m2/settings.xml \
  mvn -f /usr/src/app/pom.xml clean install

And build command such as:

DOCKER_BUILDKIT=1 docker build --secret id=mvnsettings,src=$HOME/.m2/settings.xml . 

Maven should then be able to download parent dependency during build.

Note: this is NOT COPYing the settings.xml in image, as the secret settings.xml will only be made available for the specified build step and won't be persisted in final image.

Copy com.company.parent:jee6 pom.xml during build

This solution is less practical and may not solve problem entirely:

  • It would require to have com.company.parent:jee6:pom:1.0.1-SNAPSHOT pom.xml file available in build context
  • Your parent pom.xml may refer to other private dependencies. You would have to include them the same way.

... But it still may be worth a try.

You can do something like:

FROM maven:3.5-jdk-8 AS build  

# Copy and install parent pom
COPY parent-pom.xml /tmp/parent/pom.xml
RUN mvn -f /tmp/parent/pom.xml clean install

COPY module1 /usr/src/app/src  
COPY module2 /usr/src/app/src  
COPY module3 /usr/src/app/src  
COPY pom.xml /usr/src/app  
RUN mvn -f /usr/src/app/pom.xml clean install