【问题标题】:Fail maven build when maven-antrun-plugin failed当 maven-antrun-plugin 失败时,maven 构建失败
【发布时间】:2010-11-26 05:44:26
【问题描述】:

我正在运行一个 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>  

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

<project name="example" basedir="." default="aa">
<target name="aa">
  <ant antfile="build.xml" target="test" />
</target>
</project>

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

所以我的问题是如何在antrun测试任务失败时实现maven build失败的效果。

【问题讨论】:

    标签: maven-2 ant


    【解决方案1】:

    你可能想看看 antrun 的 failonerror 属性:

    <exec executable="python" dir="${project.root}/modules" failonerror="true"></exec>
    

    Reference.

    【讨论】:

    【解决方案2】:

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

    假设您有正当理由使用 Ant 调用测试,您需要确保将 Ant 设置为在测试无效时失败。您可以通过配置JUnit task 来做到这一点。您可能希望设置的属性是 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.
    

    扩展示例以使用 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>
    

    build.xml 为:

    <?xml version="1.0"?>
    <project name="test" default="test" basedir=".">
      <target name="test">
        <fail message="Something wrong here."/>
      </target>
    </project>
    

    给出以下错误:

    [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.
    

    【讨论】:

    • 感谢您的回答!我之所以使用ant来运行测试,是因为我的应用程序的UI是在Netbeans平台上完成的。该平台提供了现成的 ant 构建文件,我不想移植到 maven。您的示例与我在运行 Netbeans 平台构建目标时所经历的一个区别是,在您的示例中存在“BUILD ERROR”,而平台构建给出了“BUILD FAILED”消息。也许maven用junit的任务“haltonerror”但没有“haltonfailure”之类的东西运行ant?