6

我们是一家 Scala/Java 商店,我们使用 Gradle 进行构建,使用 Hudson 进行 CI。我们最近在 mocha 中编写了一些带有测试的 node.js 代码。有没有办法把它包含在我们的 gradle 工作流程和 Hudson 的设置中?我查看了gradle-javascript-plugin但我无法弄清楚如何通过它运行 npm test 或 npm install 并且不知道如何使其通过 gradle-build 或 gradle-test 命令运行并且还让 Hudson 拿起它。

4

2 回答 2

3

我可以带你去那里,我也在这个任务的中游。确保您至少拥有 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

提供了一种编译我的咖啡脚本的方法,我现在可以根据提供 stdout/stderr 的 exec cmd 结果将 npm install cmd 添加到 groovy exec 请求和 barf 中

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!')
  }
}

至于 mocha 测试,我没有这方面的第一手知识,但我怀疑你可以类似地处理。

于 2012-11-29T21:40:28.210 回答
0

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

主要思想是在 docker 容器中运行 npm 任务,该容器将在构建后立即丢弃(但 node_modules 将缓存在您的构建目录中)。所以你不需要在你的 hudson/jenkins/whatever-ci 上安装 npm 并管理它的版本。

这是一个简单的示例,说明它的外观:

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其中scriptBody包含应在容器内执行的命令。

然后运行./gradlew npmContainerizedTask

于 2016-10-06T02:26:11.333 回答