tags:

views:

2137

answers:

1

Hello,

I'm running ant task that runs junit test from within maven, using maven-antrun-plugin. The invocation looks like this:

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

When the tests fail, the build continues and reports success. I tried to reproduce this behaviour with ant only (running ant from command line ant -f example.xml):

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

but in this case everything is as expected: first test failure stops the build and reports it was unsuccessful. It looks like maven does some magic (or invokes ant in another way).

So my question if how to achieve the effect of failing maven build when antrun test task fail.

Regards, Chris

+1  A: 

Your question raises one obvious question in return, why not simply use Maven to run JUnit? The surefire plugin will execute any tests (in the test phase) that were compiled in the test-compile phase into target/test-classes (usually the contents of src/test/java). There's a JavaWorld article giving an introduction to using Junit with Maven you might find helpful

Assuming you have a valid reason to use Ant to invoke the tests, you need to ensure that Ant is set up to fail if the tests are invalid. You can do this by configuring the JUnit task. The properties you may wish to set are haltonerror or haltonfailure. Alternatively you can set a property on failure and fail the Ant build yourself using the failureproperty property.


I've included two examples to demonstrate an Ant failure causing a Maven build failure. The first is a direct invocation of the fail task, the second invokes a task in a build.xml in the same manner as you have done.

This trivial example shows that an ant failure will cause a Maven build to fail:

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

Extending the example to use an ant call as you have:

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

With build.xml as:

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

Gives the following error:

[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.
Rich Seller
Thanks for your answer!The reason why I use ant to run the test is that my application's UI is done in Netbeans Platform. The platform provides ready to use ant build files, which I don't want to port to maven.One difference between your example and what I experience when running the Netbeans Platform build target is that in your example there is a "BUILD ERROR", whereas the Platform build gives a "BUILD FAILED" message. Maybe maven runs ant with something like junit's task "haltonerror" but no "haltonfailure"?
Chris J