Maven 故障保护不会抱怨指定了不存在的配置文件

Maven failsafe is not complaining that an inexistent profile was specified

我的 pom 中有两个配置文件:

 <profiles>
        <profile>
            <id>functional-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <testSourceDirectory>test/test-functional/java</testSourceDirectory>
                            <includes>
                                <include>**/*FT.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>it-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <testSourceDirectory>test/test-it/java</testSourceDirectory>
                            <includes>
                                <include>**/*IT.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        ...

我可以像这样触发这两个配置文件中的每一个:

 mvn failsafe:integration-test -Pfunctional-tests

 mvn failsafe:integration-test -Pit-tests

但是当我运行这个:

 mvn failsafe:integration-test -PrandomWord

它触发 it-tests 配置文件。我想知道为什么以及是否有办法让故障安全插件输出类似无法识别的配置文件。

感谢您的帮助


以防万一,这是我的故障保护插件:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

回答您关于它为何触发的问题 it-tests。事实上,它不会激活任何配置文件,因此使用默认插件配置,其中包含列表中有 **/*IT.java。因此,默认情况下 运行 全部 IT 测试。

这是一种通过配置文件管理插件执行的奇怪方法。我怀疑是否有合理的方法来验证您描述的配置文件名称。我会在这里推荐另一种方法。

方法 1. 使用 <id> 和 cli with @

您可以使用 id 指定插件的两次执行,然后您可以这样做:How to execute maven plugin execution directly from command line?

 <plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <id>it-tests</id>
            <phase>none</phase> <!-- detach this execution from default lifecycle -->
            <configuration>
                <testSourceDirectory>test/test-it/java</testSourceDirectory>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>functional-tests</id>
            <phase>none</phase> <!-- detach this execution from default lifecycle -->
            <configuration>
                <testSourceDirectory>test/test-ft/java</testSourceDirectory>
                <includes>
                    <include>**/*FT.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

现在您可以从命令行执行它了:

mvn failsafe:integration-test@it-tests
mvn failsafe:integration-test@functional-tests

更新:不需要指定goals,因为它只与生命周期相关,我们还是在命令行中输入它。

方法 2. 使用 <skip> 和属性

将这两个执行保留为生命周期的一部分,但通过提供跳过标志来控制执行。 IE。定义两个属性,例如skip.tests.it=trueskip.tests.ft=true 并在相关配置部分添加 <skip>${skip.tests.ft}</skip>。那么你可以做

# run with no tests by default
mvn verify 

# run with only FT
mvn verify -Dskip.tests.ft=false 

# run with all tests
mvn verify -Dskip.tests.ft=false -Dskip.tests.it=false

到 运行 完整的生命周期以及所需的测试。