我正在尝试使用提供的示例 in the grails gradle doc 创建测试任务规则但我不断收到“具有该名称的任务已存在”错误。
我的构建脚本如下:
import org.grails.gradle.plugin.tasks.* //Added import here else fails with "Could not find property GrailsTestTask"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:2.0.0"
}
}
version "0.1"
group "example"
apply plugin: "grails"
repositories {
grails.central() //creates a maven repo for the Grails Central repository (Core libraries and plugins)
}
grails {
grailsVersion = '2.3.5'
groovyVersion = '2.1.9'
springLoadedVersion '1.1.3'
}
dependencies {
bootstrap "org.grails.plugins:tomcat:7.0.50" // No container is deployed by default, so add this
compile 'org.grails.plugins:resources:1.2' // Just an example of adding a Grails plugin
}
project.tasks.addRule('Pattern: grails-test-app-<phase>') { String taskName ->
println tasks //shows grails-test-app-xxxxx task. Why?
//if (taskName.startsWith('grails-test-app') && taskName != 'grails-test-app') {
// task(taskName, type: GrailsTestTask) {
// String testPhase = (taskName - 'grails-test-app').toLowerCase()
// phases = [testPhase]
// }
//}
}
运行$gradle grails-test-integration
或者实际上 $gradle grails-test-app-xxxxxxxx
形式的任何内容都会产生错误“无法添加任务 'gradle grails-test-app-xxxxxxxx 作为具有该名称的任务已存在”。
有人可以告诉我如何解决这个错误吗?谢谢。
最佳答案
如果您不介意覆盖插件创建的任务,您可能想尝试
task(taskName, type: GrailsTestTask, overwrite: true)
一般来说,当使用可以多次调用的任务规则时(例如,如果您有多个任务,具体取决于您的规则最终添加的任务),我在实际创建任务之前使用以下测试:
if (tasks.findByPath(taskName) == null) {tasks.create(taskName)}
仅当该任务名称不存在时,才会调用 task() 构造函数。
关于gradle - Grails 梯度 "a task with that name already exists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22600314/