integrating grunt with ant

gruntjs

Solution

You could use this macro:

<macrodef name="exec-node">
    <attribute name="module" description="The name of the NodeJS module to execute"/>
    <attribute name="failonerror" default="true" description="Fail if the exit code is not 0"/>
    <element name="args" implicit="yes" description="Argument to pass to the exec task"/>
    <sequential>
        <exec executable="cmd.exe" failonerror="@{failonerror}" osfamily="winnt">
            <arg line="/c  @{module}" />
            <args/>

            <!-- Windows cmd output workaround: http://stackoverflow.com/a/10359327/227349 -->
            <!-- Forces node's stderror and stdout to a temporary file -->
            <arg line=" &gt; _tempfile.out 2&lt;&amp;1"/>

            <!-- If command exits with an error, then output the temporary file        -->
            <!-- to stdout delete the temporary file and finally exit with error level 1  -->
            <!-- so that the apply task can catch the error if @failonerror="true"        -->
            <arg line=" || (type _tempfile.out &amp; del _tempfile.out &amp; exit /b 1)"/>

            <!-- Otherwise, just type the temporary file and delete it-->
            <arg line=" &amp; type _tempfile.out &amp; del _tempfile.out &amp;"/>
        </exec>
        <exec executable="@{module}" failonerror="@{failonerror}" osfamily="unix">
            <args/>
        </exec>
    </sequential>
</macrodef>

And you can call any command: example:

<target name="jshint">
    <exec-node module="grunt">
        <arg value="jshint" />
    </exec-node>
</target>

works like a charm: also ensures the stderr is also printed, which is a common problem when calling grunt.

Problem

Are there any good tutorials for integrating grunt with ant? Our current build uses ant because we are a Java shop. However, the front-end is beginning to become a first class citizen, and we are examining using node and grunt for the front-end build. I need to integrate the front-end build with the ant build. I need to know how to normalize the exit codes for all my custom tasks as well as the built in grunt tasks and limit the console output to these predefined codes when the grunt tasks are called by ant. Any help would be greatly appreciated.

Original source

Related problems