java - 在多模块maven项目中部署具有依赖关系的子模块
问题描述
我有一个项目结构,如:
module-1
pom.xml
Dockerfile
module-2
pom.xml
module-3
pom.xml
module-4
pom.xml
Dockerfile
pom.xml
module-2
并且是和module-3
的依赖项module-1
module-4
module-4
现在我想module-1
独立部署。现在在我的父 pom 中,我添加dockerfile-maven-plugin
并添加<skip>
了 true,而对于两个子项目,我都有skip
false,因为我想部署它们。但是,当我尝试将module-1
其选择 Dockerfile 部署为module-4
. 那么我应该如何配置我的项目,以便每个模块选择它各自的 Dockefile
我的父 pom 部分如下所示:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
我的两个孩子的孩子 pom 部分看起来像:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<configuration>
<skip>false</skip>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
<contextDirectory>${basedir}/</contextDirectory>
</configuration>
</plugin>
此外,在 jenkins 构建中,我正在运行命令:mvn -pl module-1,module-2,module-3 -am clean install
我的模块 1 的 Dockerfile 看起来像
FROM openjdk:11-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} module-1.jar
CMD java $JVM_OPTS -Djava.security.egd=file:/dev/./urandom -jar /module-1.jar
需要帮忙
解决方案
您描述的内容在以下条件下正常工作:
项目结构:
project
├── module-1
│ ├── Dockerfile
│ ├── pom.xml
│ └── src
├── module-2
│ ├── pom.xml
│ └── src
├── module-3
│ ├── pom.xml
│ └── src
├── module-4
│ ├── Dockerfile
│ ├── pom.xml
│ └── src
└── pom.xml
父 POM:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>docker-build</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>docker-push</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
<contextDirectory>${basedir}/</contextDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
子模块 POM:
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
使用以下命令为模块 1构建 docker 映像:
mvn -pl module-1,module-2,module-3 -am clean install
输出:
[INFO] ---> 04ceb1feb02c
[INFO] Step 4/4 : ENTRYPOINT ["java", "-jar", "/app.jar"]
[INFO]
[INFO] ---> Running in 2d9866a14643
[INFO] Removing intermediate container 2d9866a14643
[INFO] ---> ad16766ec096
[INFO] Successfully built ad16766ec096
[INFO] Successfully tagged ***/module-1:1.0-SNAPSHOT
[INFO]
[INFO] Detected build of image with id ad16766ec096
[INFO] Building jar: ***/maven-docker/module-1/target/module-1-1.0-SNAPSHOT-docker-info.jar
[INFO] Successfully built ***/module-1:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for module-2 1.0-SNAPSHOT:
[INFO]
[INFO] module-2 ............................................ SUCCESS [ 0.797 s]
[INFO] module-3 ............................................ SUCCESS [ 0.051 s]
[INFO] module-1 ............................................ SUCCESS [ 5.019 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.016 s
[INFO] Finished at: 2021-07-30T14:47:53+03:00
[INFO] -----------------------------------------------------------------------
使用Dockerfile
frommodule-1
因为它有 4 个步骤(并且module-4
有 2 个步骤)。
检查泊坞窗图像:
$ docker image list
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
***/module-1 1.0-SNAPSHOT ad16766ec096 16 minutes ago 456MB
...
推荐阅读
- javascript - React Native - 使用状态显示/隐藏错误消息
- google-cloud-platform - GCP VPC | 字段 'region' 的值无效:'asia-northeast3'。未知区域
- if-statement - 如何在arduino上打开泵
- ios - “GIDSignIn”类型的值没有成员“hasAuthInKeychain”
- python - 基于另一列的计算创建一列
- mysql - 使用 JOIN 的 SQL 查询计数
- node.js - Express Server 连续抛出 404 错误
- git - GIT:如何将 Readme.md 文件从 master 复制到 Branch?
- generics - 包含闭包参数的方法需要错误的类型?
- python - 如何在 XML Python 中再迭代一个节点?