我有一个项目结构:
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
我的父窗体看起来像:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
我的两个孩子的孩子诗歌部分看起来像:
<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] -----------------------------------------------------------------------
从module-1
使用Dockerfile
,因为它有4个步骤(module-4
有2个步骤)。
检查docker镜像:
$ docker image list
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
***/module-1 1.0-SNAPSHOT ad16766ec096 16 minutes ago 456MB
...
那么,我们可以为父元素做的是:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
子句:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<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>${project.basedir}/</contextDirectory>
</configuration>
</plugin>
覆盖跳到false
所以现在如果我们运行mvn dockerfile:build
,它会运行docker插件并读取每个子模块的Dockerfile
。但是,如果你想将此应用于特定模块,你可以运行mvn -pl your-module dockerfile:build
,这样我们不想要的其他模块将不会在反应器摘要中运行。
也可以在子目录中添加执行