2012-08-27 85 views
16

是否有任何好的教程整合咕噜与蚂蚁?我们目前的版本使用ant,因为我们是一家Java商店。然而,前端开始成为头等公民,我们正在研究使用node和grunt来进行前端构建。我需要将前端构建与ant构建集成。我需要知道如何规范所有我的自定义任务的退出代码以及内置的grunt任务,并在咕噜任务被ant调用时将控制台输出限制为这些预定义的代码。任何帮助将不胜感激。整合咕噜与蚂蚁

回答

2

Grunt可以调用命令行,因此您可以轻松地在grunt中创建几个任务,除了通过shell执行一个ant任务外什么也不做。

grunt-shell库使它特别容易从繁重的任务,执行外部命令:https://github.com/sindresorhus/grunt-shell

既然你在谈论的自定义退出代码,不过,你很有可能要结束了写自己的自定义咕噜任务执行shell命令,然后查看响应代码(可能使用grunt.helpers.spawn帮助程序):https://github.com/gruntjs/grunt/blob/master/docs/api_utils.md#gruntutilsspawn

我的建议是?我的organization recently went through the same thing,如果可能的话,最好尽量彻底摆脱蚂蚁,彻底摆脱与JavaScript相关的项目。

Grunt有这样一个越来越有用的插件库,如果你不能复制你的ant构建文件并创建一个100%javascript解决方案,我会感到惊讶。

+5

我也在Java商店,这是一个很难卖。虽然前端对我们而言也越来越重要,但我们不太可能将JS用作服务器端代码的构建语言。更现实的方法似乎是通过自定义的ant任务调用grunt。另一方面,我没有看到有人这样做,所以... – carbontax

0

您可以使用http://abc.tools.qafoo.com/其中包括NPM模块* 1)

,那么你唯一需要的是一个自定义的目标,如:

… 

<target 
    name="-mm:compile:main~hooked" 
    extensionOf="-compile:main~hook" 
    depends=" 
     -my-compile-npm-hook 
    " 
> 

<target 
    name="-my-compile-npm-hook" 
> 
    <echo>install local grunt-cli</echo> 
    <antcall target="npm:install"> 
     <param name="in.npm.package.name" value="grunt-cli" /> 
    </antcall> 
</target> 

… 

之后,你可能会遇到在.npm/node_modules/.bin/目录别名咕噜${npm.local.modulesdir}/.bin/ ^^不会错过包括或src/main/resources/extensions/npm/npm.properties

* 1)定义属性:unfortunatly buggy与当前的node.js版本

14

您可以使用此宏:

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

的作品就像一个魅力:也保证了标准错误还印,这是一种常见调用grunt时出现问题。