在多模块 maven 项目中部署具有依赖项的子模块

我的项目结构如下:

module-1
    pom.xml
    Dockerfile
module-2
    pom.xml
module-3
    pom.xml
module-4
    pom.xml
    Dockerfile
pom.xml

module-2module-3module-1module-4

的依赖项

现在我想独立部署 module-4module-1。现在在我的父 pom 中,我添加了 dockerfile-maven-plugin 并将 <skip> 添加到 true,而对于两个子项目,我都有 skip false,因为我想部署它们。但是,当我尝试部署 module-1 时,它会为 module-4 选择 Dockerfile。那么我应该如何配置我的项目,以便每个模块选择它各自的 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

需要帮助

jordandick 回答:在多模块 maven 项目中部署具有依赖项的子模块

您描述的内容在以下条件下正常工作:

项目结构:

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

大家都在问