测试失败后如何强制maven执行下一个目标

How to force maven to execute next goals after tests failures

我正在 运行 使用 surefire 插件测试项目,然后需要使用 "site" 目标生成报告,即使有失败的测试。

问题是,默认情况下,surefire 会在测试失败的情况下强制构建失败,并且不会执行进一步的目标。 所以我将以下配置设置为万无一失:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire.version}</version>
        <configuration>
            <testFailureIgnore>true</testFailureIgnore>
        </configuration>
</plugin>

这很好用,"site" 目标始终执行,但在这种情况下,构建状态始终是 "Success",因此我无法设置电子邮件通知。

在 "test" 目标之后是否有正确的方法来执行下一个目标而不忽略 surefire 插件中的失败?

您可以使用:

  • -DskipTests=true 完全跳过测试执行
  • -Dmaven.test.failure.ignore=true 忽略测试失败
  • --fail-at-end 让构建继续 测试阶段之后,只有在整个构建周期完成时才会失败(如果有测试失败)

鉴于此声明...

This works well and the "site" goal is always executed, but in such a case the status of the build is always "Success" so that I can't set up emails notification.

...我想你想使用 --fail-at-end。根据文档,它的行为是:

--fail-at-end - if a particular module build fails, continue the rest of the reactor and report all failed modules at the end instead

I'm running tests on a project using the surefire plugin and then need to generate a report using the "site" goal, even if there are failed tests.

如果我没看错你的问题,你所做的相当于运行实现mvn site(或mvn site:stage)目标!它将在 运行 测试后生成站点,并且(如果这样配置)包括失败和未失败的报告。它不会失败,因为您没有请求 'test the project',而是请求 'build the site of the project with test results',它成功地做到了。

... the status of the build is always "Success" so that I can't set up emails notification.

根据您的描述,我假设发送通知是基于构建状态,并且通知有一个 link 到生成的站点。如果是这种情况,我可以想到 3 个选项,它们可能会满足您的需求:

  • 更改由 surefire 插件的输出(通常在 ${basedir}/target/surefire-reports 中)而不是构建状态触发的通知。然后你可以简单地 运行 mvn site
  • 不知何故 publish/share surefire 输出文件(如果需要转换它们)并使通知指向它们而不是指向站点。然后你可以简单地 运行 mvn test
  • 运行 mvn site test 将首先生成站点(运行 宁测试并忽略构建报告的失败)然后 运行 测试(再次)使您​​的失败构建