我正在运行一个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构建失败的效果。
发布于 2012-02-16 08:31:51
您可能希望查看antrun的failonerror属性:
<exec executable="python" dir="${project.root}/modules" failonerror="true"></exec>Reference。
发布于 2009-09-07 14:16:44
你的问题引出了一个显而易见的问题,为什么不简单地使用Maven来运行JUnit?surefire plugin将执行在测试编译阶段编译成目标/测试类(通常是src/ test /java的内容)的任何测试(在测试阶段)。有一个JavaWorld article介绍了如何在Maven中使用Junit,你可能会觉得很有帮助
假设您有充分的理由使用Ant调用测试,则需要确保Ant设置为在测试无效时失败。您可以通过配置JUnit task来完成此操作。您可能希望设置的属性是haltonerror或haltonfailure。或者,您可以在失败时设置一个属性,并使用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.https://stackoverflow.com/questions/1389384
复制相似问题