1

I have a Maven Java application that I am trying to dockerize. I have the following dockerfile so far, that I copied from this answer.

#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean install

EXPOSE 8080 9990

COPY --from=build /usr/src/app/target/helloworld-1.0.0-SNAPSHOT.jar /opt/jboss/jboss-eap-6.2/standalone/deployments/

This would be fine if my application was a single module project, however because it is multi-module, I assume that I cannot just use COPY src /home/app/src.

How can I change my dockerfile to account for my multi-module project?

I have tried the following:

#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY module1 /home/app
COPY module2 /home/app
COPY module3  /home/app
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean install

EXPOSE 8080 9990
    
COPY --from=build /usr/src/app/target/helloworld-1.0.0-SNAPSHOT.jar /opt/jboss/jboss-eap-6.2/standalone/deployments/

However this gives error at the mvn clean install stage:

[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for myapp:21.01-SNAPSHOT: Could not find artifact jee6:pom:1.0.1-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 8, column 10

Note that jee6:pom:1.0.1-SNAPSHOTis from my local m2 folder.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
java12399900
  • 1,485
  • 7
  • 26
  • 56
  • Does this answer your question? [docker with maven jar](https://stackoverflow.com/questions/58675978/docker-with-maven-jar) – Joe Dec 23 '20 at 12:37

2 Answers2

1

You could have an additional integration module, with a dependency on the other modules, which is responsible for gathering components into the Docker image. Also a good place to put integration tests.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • I have tried to copy over all of the modules and build then. can you please check the question edit for error. – java12399900 Dec 23 '20 at 12:17
  • Can you please give more information on this? thanks – java12399900 Dec 24 '20 at 11:44
  • @Raedwald I have tried to do what you have advised to put all modules into integration module but I still cant get it build in docker. here is my question https://stackoverflow.com/questions/70273777/how-to-build-multimodule-maven-project-with-docker – user9347049 Dec 08 '21 at 12:17
1

I would just use the maven jib plugin instead of hand crafting a Dockerfile. Much easier and it should work with multi module projects too I reckon

allkenang
  • 1,511
  • 15
  • 12
  • Do you have an example of how to use? – java12399900 Dec 23 '20 at 13:21
  • Looking at your codes, you are trying to deploy it into a JBoss application server right? – allkenang Dec 23 '20 at 14:08
  • That makes it more complicated as the Jboss application server itself needs to be in the docker image. Also the use case for using Docker images in this case is not as compelling as Docker images should be as lean as possible. Your docker image approach would likely make more sense if you use something like Spring Boot for instance to run your application. – allkenang Dec 23 '20 at 14:38
  • Ok, well in this case then what would you recommend? I want to build the maven project and then run it on eap – java12399900 Dec 24 '20 at 11:21
  • Then just use a typical EAR or WAR deployment. Forget dockerizing it. If you are not using EJBs then a WAR project works best coz it's just much easier. Here is a link that you can use: http://www.sgaemsolutions.com/2019/06/preparing-your-spring-boot-app-to.html – allkenang Dec 25 '20 at 06:15