Grails gradle“具有该名称的任务已经存在”

Posted

技术标签:

【中文标题】Grails gradle“具有该名称的任务已经存在”【英文标题】:Grails gradle "a task with that name already exists" 【发布时间】:2014-05-01 06:33:29 【问题描述】:

我正在尝试使用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 添加为具有该名称的任务已经存在”。

谁能告诉我如何解决这个错误?谢谢。

【问题讨论】:

您是否尝试过使用不同的任务名称模式?您是否尝试在添加任务之前检查它是否存在? 嗨,彼得,是的,我尝试了不同的模式,但它总是失败并出现错误“无法添加任务...同名的任务已经存在。” 奇怪。问题可能与grails-gradle-plugin(我不熟悉)有关。 我最好的猜测是grails-gradle-plugin 本身添加了一个委托给 Grails 构建系统的“catch all”任务规则。在这种情况下,您可能无法在使用此插件时声明自己的任务规则。 @PeterNiederwieser:我认为你是对的。查看插件的源代码,有一个 addRule 可以为前缀为“grails-”的模式创建任务。有没有办法可以创建与插件之前相同模式的任务? IE。在 build.gradle 中,如何在插件之前创建 grails-XXX 形式的任务?还是不明智? 【参考方案1】:

如果您不介意覆盖插件创建的任务,您可能想尝试

task(taskName, type: GrailsTestTask, overwrite: true)

一般来说,当使用可以多次调用的任务规则时(例如,如果您有多个任务,具体取决于您的规则最终添加的任务),我会在实际创建任务之前使用以下测试:

if (tasks.findByPath(taskName) == null) tasks.create(taskName)

仅当此任务名称不存在时才会调用 task() 构造函数。

【讨论】:

覆盖任务”是什么意思?这是否意味着当另一个代码调用它时,任务的当前执行被否定了。 表示将插件提供的任务替换为您的任务定义。被处决的是你的。有关详细信息,请参阅文档:docs.gradle.org/current/userguide/…

以上是关于Grails gradle“具有该名称的任务已经存在”的主要内容,如果未能解决你的问题,请参考以下文章

Gradle / Grails 应用程序

如何使用 Gradle 设置多项目 Grails?

如何告诉 Grails 使用 Gradle 解决依赖关系

GRAILS ---命令 grails war 和 gradle build 有啥区别?

Gradle 在构建 Grails 项目时给出 ClassNotFoundException

使用 Gradle 运行时设置 Grails 端口