Grails gradle "a task with that name already exists"

user3240644 picture user3240644 · Mar 24, 2014 · Viewed 14.9k times · Source

I'm trying to create a test task rule using the example provided in the grails gradle doc but I keep getting "a task with that name already exists" error.

My build script is as follows:

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]
    //    }
    //}
}

Running $gradle grails-test-integration or in fact anything of the form $gradle grails-test-app-xxxxxxxx yields the error "Cannot add task 'gradle grails-test-app-xxxxxxxx as a task with that name already exists".

Can someone please advise how I can resolve this error? Thanks.

Answer

johnmartel picture johnmartel · Sep 8, 2014

If you don't mind overriding the task created by the plugin, you might want to try

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

In general, when using task rules that can be called multiple times (for instance if you have multiple tasks depending on a task eventually added by your rules), I use the following test before actually creating the task:

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

This will call the task() constructor only if this task name does not exists.