Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 2 maven antrun插件失败时maven构建失败_Maven 2_Ant - Fatal编程技术网

Maven 2 maven antrun插件失败时maven构建失败

Maven 2 maven antrun插件失败时maven构建失败,maven-2,ant,Maven 2,Ant,我正在运行一个Ant任务,它使用maven antrun插件从maven内部运行junit测试。调用如下所示: <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>ant-test</id> <phase>test</phase>

我正在运行一个Ant任务,它使用maven antrun插件从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>  

maven antrun插件
蚂蚁试验
测试
跑
当测试失败时,构建将继续并报告成功。我试图仅用ant重现这种行为(从命令行“ant-f example.xml”运行ant):


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


因此,我的问题是,当antrun测试任务失败时,如何实现maven构建失败的效果。

你的问题提出了一个显而易见的问题,为什么不简单地使用maven运行JUnit呢?将执行在测试编译阶段编译为目标/测试类(通常是src/test/java的内容)的任何测试(在测试阶段)。这里有一篇介绍如何将Junit与Maven结合使用的文章,您可能会觉得这很有帮助

假设您有一个使用Ant调用测试的有效理由,那么您需要确保Ant被设置为在测试无效时失败。您可以通过配置来实现这一点。您可能希望设置的属性是haltonerror或haltonfailure。或者,您可以在失败时设置属性,并使用failureproperty属性使Ant构建失败


我已经包含了两个示例来演示导致Maven构建失败的Ant故障。第一个是对fail任务的直接调用,第二个是以与您相同的方式在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.

您可能需要查看antrun的FailOneError属性:

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


.

谢谢你的回答!我之所以使用ant来运行测试,是因为我的应用程序的UI是在Netbeans平台上完成的。该平台提供了随时可用的ant构建文件,我不想将其移植到maven。您的示例与我在运行Netbeans平台构建目标时的体验之间的一个区别是,在您的示例中存在“构建错误”,而平台构建给出了“构建失败”消息。也许maven用junit的任务“haltonerror”运行ant,但没有“haltonfailure”?几年后。。。我可能是RTFM,谢谢:-)(在标准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>
<?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.
<exec executable="python" dir="${project.root}/modules" failonerror="true"></exec>