Maven tips and Tricks: Avoid running tests twice

Sometimes you may find that your tests are run twice, once to make
the report for the site (thus not breaking the build if they fail) and
second time to create a jar, war or any other artifact, and probably in
this step you want the build to break if tests don’t succeed. This is
very common in a continuous integration scenario where the machine with
CruiseControl or any other CI tool deploys the site and generated
artifacts.

My suggestion is to create a goal in your maven.xml like this

    <goal name="continuousintegration">
        <attainGoal name="clean"/>
        <attainGoal name="site:deploy"/>
        <!-- avoid running tests again -->
        <j:if test="${maven.test.failure}">
          <fail message="There were test failures."/>
        </j:if>
        <j:set var="maven.test.skip" value="true"/>
        <attainGoal name="jar:deploy"/>
        <j:set var="maven.test.skip" value="false"/>
    </goal>

The trick is checking the maven.test.failure property after creating
the site, if it’s true the build has to fail and if it’s false you can
skip tests in next steps.

2 thoughts on “Maven tips and Tricks: Avoid running tests twice

  1. One of most useful tricks I’ve ever seen, I was going to hack few plugins to achieve this goal but with this one it’ll be much simpler.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s