Ant – Execute Grunt and fail it on error

I want to execute a Grunt build in an Ant build project. This could be done by using <exec>.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Called Grunt" default="grunt" basedir=".">
  <target name="grunt">
    <exec executable="grunt" dir="${basedir}">
      <arg value="build"/>
    </exec>
  </target>
</project>

 

It works quite well, but then i realize if there is error in the grunt execution, Ant will still mark the Ant build successful and this may lead to some downstream failure in a continuous integration workflow.

To avoid that, i add a checking on the Grunt output.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Called Grunt" default="grunt" basedir=".">
  <target name="grunt">
    <exec executable="grunt" dir="${basedir}" outputproperty="log">
      <arg value="kit"/>
    </exec>

    <echo message="${log}"/>
    <condition property="onSuccess">
      <matches pattern="Done, without errors." string="${log}"/>
    </condition>
    <fail message="Grunt failed!" unless="onSuccess"/>
  </target>
</project>

 

The idea is to collect the Grunt output in outputproperty and check if the string Done, without errors. exists.

Done =)

Reference:

Advertisement

4 thoughts on “Ant – Execute Grunt and fail it on error”

  1. Hi, I’m experiencing this same kind of issue in a project where unit tests are written in jasmine and executed using karma.

    First of all I managed to perform these karma unit tests using grunt, and it worked like a charm.

    Next step should be to launch “grunt test” from an Ant target within a build.xml file.

    I’m writing Ant code for my project using Eclipse IDE, and even if the console keeps telling me “BUILD SUCCESSFULL” I actually have written my karma tests to fail, just like you did and I’m not able to see the karma output that warns me about the fails.

    Can you suggest me a way to print the karma output into the Ant/Eclipse console?

    Like

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.