如何将NodeJStesting包含到Gradle和Hudson中?

我们是一个Scala / Java商店,我们使用Gradle构build,Hudson使用CI。 我们最近用摩卡testing了一些node.js代码。 无论如何要把它包含在我们的Gradle工作stream程中,并在Hudson中进行设置? 我查看了gradle-javascript-plugin,但是我不知道如何通过它运行npm test或npm install,也不知道如何通过gradle-build或gradle-test命令来运行它,并且让Hudson捡起它。

我可以把你带到那里,我也在这个任务的中间。 确保你至less有Gradle 1.2。

import org.gradle.plugins.javascript.coffeescript.CoffeeScriptCompile apply plugin: 'coffeescript-base' repositories { mavenCentral() maven { url 'http://repo.gradle.org/gradle/javascript-public' } } task compileCoffee(type: CoffeeScriptCompile){ source fileTree('src') destinationDir file('lib') } 

参考: http : //gradle.1045684.n5.nabble.com/State-of-javascript-stuff-in-master-td5709818.html

提供了一种方法来编译我的coffeescript我现在可以将npm install cmd添加到groovy exec请求和barf中,具体取决于提供stdout / stderr的exec cmd结果

 npm install echo $? 0 npm install npm ERR! install Couldn't read dependencies npm ERR! Failed to parse json npm ERR! Unexpected token } npm ERR! File: /<>/package.json npm ERR! Failed to parse package.json data. npm ERR! package.json must be actual JSON, not just JavaScript. npm ERR! npm ERR! This is not a bug in npm. npm ERR! Tell the package author to fix their package.json file. JSON.parse npm ERR! System Darwin 11.4.2 npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" npm ERR! cwd /<>/ npm ERR! node -v v0.8.14 npm ERR! npm -v 1.1.65 npm ERR! file /<>/package.json npm ERR! code EJSONPARSE npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /<>/npm-debug.log npm ERR! not ok code 0 echo $? 1 

结果是:

 task npmDependencies { def proc = 'npm install'.execute() proc.in.eachLine { line -> println line} proc.err.eachLine { line -> println 'ERROR: '+line } proc.waitFor() if (proc.exitValue()!=0){ throw new RuntimeException('NPM dependency installation failed!') } } 

至于摩卡testing,我没有这方面的第一手知识,但我怀疑你可以处理类似。

如果你喜欢泊坞窗,你可能会喜欢这个gradle插件: https : //github.com/dimafeng/containerized-tasks

主要的想法是在Docker容器内运行你的npm任务,这个容器在构build之后立即抛出(但是node_modules将被caching在你的build目录中)。 所以你不需要在你的hudson / jenkins / whatever-ci上安装npm并pipe理它的版本。

下面是一个简单的例子:

 plugins { id "com.dimafeng.containerizedTask" version "0.4.0" } npmContainerizedTask { sourcesDir = 'test-env/gulp' outputLevel = 'INFO' // ALL, DEBUG scriptBody = 'npm install\ngulp' } 

其中, sourcesDir是一个包含你的package.json的目录, sourcesDir是应该在容器中执行的命令。

然后运行./gradlew npmContainerizedTask