您描述的内容在以下条件下正常工作:
项目结构:
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
中的 module-1
,因为它有 4 个步骤(而 module-4
有 2 个步骤)。
检查泊坞窗图像:
$ docker image list
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
***/module-1 1.0-SNAPSHOT ad16766ec096 16 minutes ago 456MB
...
,
所以,我们可以为父 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>
<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
以便我们不需要的其他模块不会在反应器摘要中运行。
另外,你可以在你想要的孩子中添加执行
本文链接:https://www.f2er.com/10985.html