首页 > 解决方案 > Maven插件跨越执行顺序

问题描述

我正在使用故障安全插件编写一些集成测试。

我想执行以下操作:

1)启动Docker(目标在预集成测试阶段启动)

2)Start Spring(目标在预集成测试阶段开始)

3)测试(阶段集成测试)

4)停止Spring(集成测试后的目标停止)

5) 停止 Docker(集成测试后的目标停止)

为什么?我想在 Spring 之前启动 Docker,以便在 Spring 启动时所有数据库都准备就绪,并且我想在 Spring 之后停止 Docker,以避免由于数据库连接丢失而在 Spring 中出现很多错误。

我有以下 pom.xml:

        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.20.1</version>
            <executions>
                <execution>
                    <id>run-docker-containers</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        ...
                    </configuration>
                </execution>
                <execution>
                    <id>stop-docker-containers</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        ...
                    </configuration>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

但是有了这个 pom.xml 我得到了这个命令:

1) 启动 Docker

2) 开始春天

3) 测试

4) 停止 Docker

5) 停止弹簧

也就是说,Docker 在 Spring 之前停止,我不希望这样。

我知道 Maven 中的执行顺序是由 pom.xml 中的顺序给出的,但在这种情况下,我需要跨越目标。

有什么建议吗?

谢谢。

标签: mavenmaven-3maven-pluginmaven-failsafe-pluginmaven-docker-plugin

解决方案


Maven 中的执行顺序基于两个组件:

  • 一、执行绑定的生命周期阶段
  • 对于绑定到同一阶段的执行,POM 中的排序优先。

Maven 不允许对同一个插件进行两次配置,因此无法将 Docker Stop 执行移至 Spring 以下。我知道解决此类问题的唯一方法是在混合中引入第三阶段。将“停止”目标绑定到阶段看起来有点奇怪verify,我肯定会添加评论来解释为什么这样做是为了后代 - 但它确实有效。

此插件配置执行以下操作(假设您使用maven-failsafe-pluginfor 集成测试;如果您使用不同的插件,请遵循与该插件相同的原则)。括号中的 Maven 生命周期阶段:

1) Docker 启动(预集成测试)

2)Spring Start(预集成测试;Docker之后的Spring插件配置)

3)执行测试(集成测试)

4)Spring Stop(集成测试后)

5)Docker停止(验证)

6)验证测试结果(verify; Failsafe plugin config after Docker)

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.20.1</version>
    <executions>
        <execution>
            <id>run-docker-containers</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-docker-containers</id>
            <phase>verify</phase>    <!-- important: note phase -->
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>${version.maven.failsafe.plugin}</version>
    <executions>
        <execution>
            <id>failsafe-integration-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>failsafe-verify</id>
            <phase>verify</phase>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

推荐阅读