Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integrating grunt with ant

Tags:

gruntjs

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.

like image 319
user1120155 Avatar asked Aug 27 '12 18:08

user1120155


2 Answers

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.

like image 89
R. Oosterholt Avatar answered Sep 27 '22 20:09

R. Oosterholt


Grunt can call out to the command line, so you could easily create several tasks in grunt that do nothing but execute an ant task via the shell.

The grunt-shell library makes it especially easy to execute external commands from a grunt task: https://github.com/sindresorhus/grunt-shell

Since you're talking about custom exit codes, though, you'll probably have to end up writing your own custom grunt task that executes a shell command and then looks at the response code (perhaps using the grunt.helpers.spawn helper): https://github.com/gruntjs/grunt/blob/master/docs/api_utils.md#gruntutilsspawn

My advice? My organization recently went through the same thing and it's best if possible to just make a clean break from ant and get rid of it entirely for your JavaScript-related projects.

Grunt has such a growing and useful library of plugins I'd be surprised if you couldn't duplicate your ant build files and create a 100% javascript solution.

like image 38
smithclay Avatar answered Sep 27 '22 20:09

smithclay