当maven-antrun-plugin失败时,maven构建失败

10 ant maven-2

我正在运行一个Ant任务,使用maven-antrun-plugin在maven中运行junit测试.调用如下所示:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <id>ant-test</id>
      <phase>test</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <tasks unless="maven.test.skip">
          <ant antfile="${basedir}/build.xml" target="test">
            <property name="build.compiler" value="extJavac" />
          </ant>
        </tasks>
      </configuration>
    </execution>
</executions>
</plugin>  
Run Code Online (Sandbox Code Playgroud)

当测试失败时,构建继续并报告成功.我尝试仅使用ant重现此行为(从命令行'ant -f example.xml'运行Ant):

<project name="example" basedir="." default="aa">
<target name="aa">
  <ant antfile="build.xml" target="test" />
</target>
</project>
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,一切都如预期:第一次测试失败停止构建并报告它不成功.它看起来像maven做了一些魔术(或以另一种方式调用蚂蚁).

所以我的问题是如何在解决测试任务失败时如何实现失败的maven构建的效果.

Lui*_* R. 11

您可能想要查看antrun的failonerror属性:

<exec executable="python" dir="${project.root}/modules" failonerror="true"></exec>
Run Code Online (Sandbox Code Playgroud)

参考.


Ric*_*ler 4

您的问题反过来又提出了一个明显的问题:为什么不简单地使用 Maven 来运行 JUnit?Surefire插件将执行在测试编译阶段编译到目标/测试类(通常是 src/test/java 的内容)中的任何测试(在测试阶段)。有一篇JavaWorld 文章介绍了如何将 Junit 与 Maven 结合使用,您可能会觉得有帮助

假设您有充分的理由使用 Ant 调用测试,则需要确保 Ant 设置为在测试无效时失败。您可以通过配置JUnit 任务来完成此操作。您可能希望设置的属性是haltonerrorhaltonfailure或者,您可以在失败时设置一个属性,并使用failureproperty属性自行使 Ant 构建失败。


我提供了两个示例来演示导致 Maven 构建失败的 Ant 故障。第一个是直接调用失败任务,第二个以与您所做的相同的方式调用 build.xml 中的任务。

这个简单的示例表明 ant 失败将导致 Maven 构建失败:

<plugins>
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>test</phase>
        <configuration>
          <tasks>
            <fail message="Something wrong here."/>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: Something wrong here.
Run Code Online (Sandbox Code Playgroud)

扩展示例以使用 ant 调用,如下所示:

<plugins>
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>test</phase>
        <configuration>
        <tasks unless="maven.test.skip">
          <ant antfile="${basedir}/build.xml" target="test">
            <property name="build.compiler" value="extJavac" />
          </ant>
        </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)

使用 build.xml 作为:

<?xml version="1.0"?>
<project name="test" default="test" basedir=".">
  <target name="test">
    <fail message="Something wrong here."/>
  </target>
</project>
Run Code Online (Sandbox Code Playgroud)

给出以下错误:

[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks

test:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: The following error occurred while executing this line:
C:\test\anttest\build.xml:4: Something wrong here.
Run Code Online (Sandbox Code Playgroud)