Docker上的多模块Maven项目 [英] multi-module Maven project on Dockers
问题描述
我有一个多模块Maven项目,其中的每个模块都是可运行的微服务应用程序,包含它们自己的Dockerfile,因此在生产中,每个模块都是容器化的应用程序.
I have a multi-module maven project where the single modules are all runnable microservice applications containing their own Dockerfile, so in production every module will be a containerized application.
包含子模块的父项目仅包含父pom.xml和docker-compose.yml
The parent project, which contains the child-modules only contains the parent pom.xml and the docker-compose.yml
我尝试使用以下Dockerfile(在子模块级别):
I have tried to use the following Dockerfile (on sub-module level):
FROM sgrio/java-oracle
RUN apt-get update
RUN apt-get install -y maven
COPY ../pom.xml /usr/local/service/Oogaday/pom.xml
COPY pom.xml /usr/local/service/Oogaday/OogadayApi/pom.xml
COPY src /usr/local/service/Oogaday/OogadayApi/src
WORKDIR /usr/local/service/Oogaday/OogadayApi/
RUN mvn package -DskipTests
CMD ["java","-jar","org.oogaday.api-1.0-SNAPSHOT-jar-with-dependencies.jar"]
但是我收到一个安全错误,因为我正在尝试复制父pom.xml文件(该文件未放置在我从其运行构建的目录中).
But I am getting a security error because I am trying to copy the parent pom.xml file (which is not placed in the directory from which I am running the build).
那么有没有一种方法可以使用父pom构建基于Maven的子模块?
So is there a way to build a maven based sub-module with parent pom?
推荐答案
这是我的建议,您应该充分利用docker缓存的优势.
This is my suggestion, you should take advantage the more you can of docker cache.
假定此多模块项目pom布局:
Assume this multi-module project pom layout:
+-- my-project
+-- module1
| `-- pom.xml
+-- module2
| `-- pom.xml
`- pom.xml
Dockerfile:
Dockerfile:
# cache as most as possible in this multistage dockerfile.
FROM maven:3.6-alpine as DEPS
WORKDIR /opt/app
COPY module1/pom.xml module1/pom.xml
COPY module2/pom.xml module2/pom.xml
# you get the idea:
# COPY moduleN/pom.xml moduleN/pom.xml
COPY pom.xml .
RUN mvn -B -e -C org.apache.maven.plugins:maven-dependency-plugin:3.1.2:go-offline
# if you have modules that depends each other, you may use -DexcludeArtifactIds as follows
# RUN mvn -B -e -C org.apache.maven.plugins:maven-dependency-plugin:3.1.2:go-offline -DexcludeArtifactIds=module1
# Copy the dependencies from the DEPS stage with the advantage
# of using docker layer caches. If something goes wrong from this
# line on, all dependencies from DEPS were already downloaded and
# stored in docker's layers.
FROM maven:3.6-alpine as BUILDER
WORKDIR /opt/app
COPY --from=deps /root/.m2 /root/.m2
COPY --from=deps /opt/app/ /opt/app
COPY module1/src /opt/app/module1/src
COPY module2/src /opt/app/module2/src
# use -o (--offline) if you didn't need to exclude artifacts.
# if you have excluded artifacts, then remove -o flag
RUN mvn -B -e -o clean install -DskipTests=true
# At this point, BUILDER stage should have your .jar or whatever in some path
FROM openjdk:8-alpine
WORKDIR /opt/app
COPY --from=builder /opt/app/<path-to-target>/my-1.0.0.jar .
EXPOSE 8080
CMD [ "java", "-jar", "/opt/app/my-1.0.0.jar" ]
这篇关于Docker上的多模块Maven项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!