17

grunt を ant と統合するための優れたチュートリアルはありますか? 私たちは Java ショップであるため、現在のビルドでは ant を使用しています。しかし、フロントエンドは一級市民になりつつあり、フロントエンドのビルドに node と grunt を使用することを検討しています。フロントエンド ビルドを ant ビルドに統合する必要があります。すべてのカスタム タスクと組み込みの grunt タスクの終了コードを正規化し、ant によって grunt タスクが呼び出されたときにコンソール出力をこれらの定義済みコードに制限する方法を知る必要があります。どんな助けでも大歓迎です。

4

3 に答える 3

15

このマクロを使用できます:

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

また、任意のコマンドを呼び出すことができます: 例:

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

魅力のように機能します: また、stderr も出力されるようにします。これは、grunt を呼び出すときによくある問題です。

于 2013-06-26T14:31:02.890 に答える
2

Grunt はコマンド ラインを呼び出すことができるので、シェル経由で ant タスクを実行するだけのいくつかのタスクを grunt で簡単に作成できます。

このgrunt-shellライブラリにより、単調なタスクから外部コマンドを実行することが特に簡単になります: https://github.com/sindresorhus/grunt-shell

grunt.helpers.spawnただし、カスタムの終了コードについて話しているので、シェル コマンドを実行して応答コードを確認する (おそらくヘルパーを使用して) 独自のカスタム grunt タスクを作成する必要があるでしょう: https://github .com/gruntjs/grunt/blob/master/docs/api_utils.md#gruntutilsspawn

私のアドバイス?私の組織は最近同じことを経験しました。可能であれば、ant から完全に切り離して、JavaScript 関連のプロジェクトから完全に取り除くことが最善です。

Grunt には、プラグインの非常に成長している便利なライブラリがあります。Ant ビルド ファイルを複製して、100% JavaScript ソリューションを作成できなかったとしたら、私は驚くでしょう。

于 2012-10-02T23:37:16.653 に答える