I'm kinda new with Docker but I need to build my microservice applications and I have a structure where some of those applications use another application from my package, another "module" from IntalliJ's project view, it's defined on Maven's POM like this:
<!-- Annerd Dependencies -->
<dependency>
<groupId>com.annerd</groupId>
<artifactId>ams-commons-utilities</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency>
But when I build the my Dockerfile, it gives this error
50.53 [ERROR] Failed to execute goal on project ams-findme: Could not resolve dependencies for project com.annerd.findme:ams-findme:jar:BETA-2.1: The following artifacts could not be resolved: com.annerd:ams-commons-utilities:jar:1.2.0 (absent): Could not find artifact com.annerd:ams-commons-utilities:jar:1.2.0 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
Which leads me to believe that I'm missing some steps here.
This is my Dockerfile
FROM maven:3.9.6-amazoncorretto-17-al2023 AS build-stage
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD . /usr/src/app
RUN mvn install -U
###############################################################
FROM openjdk:17-alpine AS production-stage
COPY --from=build-stage /usr/src/app/target/*.jar annerd-find-me-api.jar
EXPOSE 8785
ENTRYPOINT ["java", "-jar", "annerd-find-me-api.jar", "-Xmx256m -Xms128m -Dspring.profiles.active=prod"]
My question is, the project builds correctly on IntelliJ's IDE with the provided POM, I don't have any repository server like Nexus or similar to provide this dependency which is what I'm suspecting I need, I'm planning on implementing one but still not there yet, is there a way to solve this problem without a repository? Am I missing something?
You mentioned it builds locally so it should work if that artifact is exists in the right path.
First of all, give a try to change your local maven repository of that maven project. I'm kinda suspecting that the project's repo is configured towards the other application as you mentioned in your question. If not, ignore this point.
Is there any error message during docker build? If not, check whether the artifact exists in the path /usr/src/app/target
.
If so, copy that to specific artifact or all if you need since you used wildcard.
COPY --from=build-stage /usr/src/app/target/*.jar .
COPY --from=build-stage /usr/src/app/target/annerd-find-me-api.jar annerd-find-me-api.jar
According to our discussion under comments section, I understand the dependency is from another project and the module is not under your current project.
Let's say, you work on project-A but the module is belonging project-B, which your Dockerfile
in project-A can't find that module because you add all into the layer from current path as follows ADD . /usr/src/app
.
In your Dockerfile
, specify the module path out of project-A, which points the path under project-B such as:
FROM maven:3.9.6-amazoncorretto-17-al2023 AS build-stage
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD . /usr/src/app
COPY </path-to-project-B/annerd-find-me-api.jar> /usr/src/app/target/annerd-find-me-api.jar <=== This line changed
RUN mvn install -U
FROM openjdk:17-alpine AS production-stage
...
...
Bear mind that, the path to the actual module or artifact that you generated should be either static or dynamic file path up to your needs.
For instance, if the project-B path or module path can be changed dynamically, then you should define such as ~/project/project-B/target/module-name.jar
or similar.