Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
Dockerize a multi maven project (not multi-module)

In my maven application i have multiple projects:

  • Core
  • Application 1
  • Application 2

Application 1 and Application 2 are two projects that uses the core (for example, those application are built for two different customers)

In order to Dockerize all of them, the simplest way would be to create a multi-module project, but the downside is that i have all inside a single project (core + Application 1 + Application 2).

I would like to have the core separated from them.

The main problem with this configuration is that the core project need to built before the other two, and App 1 and App 2 use this as maven dependency:

App 1

 <dependency>
    <groupId>it.myorg</groupId>
    <artifactId>core-project</artifactId>
    <version>1.12.0-SNAPSHOT</version>
 </dependency>

If i try to dockerize the App 1 its fail when i package it, because inside the docker container core-project 1.12.0-SNAPSHOT do not exists.

I was thinking to setup a "local maven repo", pushing the core there and App 1 will "pull" the jar from the repo and not from .m2 folder, but i dont like this soulution.

I can provide more information, sorry if i dont provide examples, but my paper is white right now :(

Folder structure

+- Core
--- pom.xml
--- src

+- Application1
--- pom.xml
--- src

The solution i'm trying to do now is create a Dockerfile for core project (FROM maven:latest), building the image with a tag and in Dockerfile of App1 use this image (so, creating a multi stage build but in two separate moments).

The best would be

FROM maven:latest as core-builder
## build the core

FROM maven:latest
## Copy jar from builder

Because the project are in separate folder, i cant build the core in this way. I need to build del core BEFORE (running docker build -t) and later copy from them.

UPDATE

After the correct answer from @mihai i'm asking if its possible a structure like this:

-- myapp-docker
  - Dockerfile
  - docker-compose.yml
-- core-app
-- application_1

Having Dockerfile at the same level of core-app and application_1 its totally fine and 100% working. The only "problem" is that i should put all the projects in the same repo.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is the proposed solution with multi-stage builds.

To replicate your setup I created this structure:

.
├── Dockerfile-app1
├── application1
│   ├── pom.xml
│   └── src
│       └── main
│           ├── resources
│           └── webapp
│               ├── WEB-INF
│               │   └── web.xml
│               └── index.jsp
├── core
│   ├── pom.xml
│   └── src
│       ├── main
│       │   └── java
│       │       └── com
│       │           └── test
│       │               └── App.java
│       └── test
│           └── java
│               └── com
│                   └── test
│                       └── AppTest.java

In the pom.xml file from Application 1 I added the dependency to core:

    <dependency>
      <groupId>com.test</groupId>
      <artifactId>core</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

I named the Dockerfile Dockerfile-app1, this way you can have more than 1 of them.

This is the Dockerfile-app1:

FROM maven:3.6.0-jdk-8 as build

WORKDIR /apps

COPY ./core .
RUN mvn clean install

FROM maven:3.6.0-jdk-8

# If you comment this out then the build fails because it cannot find the dependency to 'core'
COPY --from=build /root/.m2 /root/.m2

COPY ./application1 ./

RUN mvn clean install

You should probably add an entrypoint at the end to run your project or even better add another 3rd stage that only copies the generated artefacts and runs your project (this way the final image will not have your sourced in).

The first stage only builds the core submodule.

The second stage used the results of the first stage, copies only the source for application1 and builds it.

You can easily replicate this for application2 by creating a similar file Dockerfile-app2.

over 4 years ago · Santiago Trujillo Report

0

Since you're using maven, try dockerfile-maven to build the image. You don't want any of your build information inside of your image (like what the dependencies are), you should just add the jar at the end. I usually use it together with spring-boot-maven-plugin and repackage, to get a fully self-contained jar.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!