You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

maven-surefire-plugin测试失败后如何确保exec-maven-plugin执行Shell脚本?

测试失败时确保exec-maven-plugin执行脚本的解决方案

这个需求完全可以实现,你遗漏的核心配置是exec-maven-plugin的<alwaysExecute>参数,同时需要配合maven-surefire-plugin和Maven命令参数的调整,具体操作如下:

1. 调整maven-surefire-plugin配置

在pom.xml中给surefire插件添加<testFailureIgnore>true</testFailureIgnore>,允许测试失败后不直接中断构建,让后续阶段能继续执行:

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

2. 配置exec-maven-plugin的执行规则

将exec插件绑定到verify阶段(该阶段在测试完成后触发),并开启<alwaysExecute>true</alwaysExecute>,确保无论前面阶段是否失败,脚本都会执行:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <id>upload-test-report</id>
      <phase>verify</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>/path/to/your/upload-script.sh</executable>
        <alwaysExecute>true</alwaysExecute>
      </configuration>
    </execution>
  </executions>
</plugin>

3. Jenkins流水线执行命令

保持使用mvn install -fae命令,-fae参数会让Maven在所有模块的所有阶段执行完成后,再整体判定构建结果,避免中途中断导致exec插件未触发。

为什么之前的尝试没生效?

  • 仅用-fae但没开testFailureIgnore:测试失败后Maven会标记该模块失败,后续阶段可能不会执行
  • 没给exec插件设置<alwaysExecute>true</alwaysExecute>:即使阶段走到了verify,若前面有失败,插件默认会跳过执行
  • 绑定的阶段不对:如果把exec绑定到install之前的阶段,可能在测试失败后被中断

内容的提问来源于stack exchange,提问作者Matan Noach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:07:26