I am getting the error
Cannot add task ':webserver:build' as a task with that name already exists.
The weird thing is my hello task is fine but my build task is not AND YES, I am trying to override the Java plugin’s build task.
Master build.gradle file:
allprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
task hello << { task -> println "I'm $task.project.name" }
task build << { task -> println "I'm building now" }
}
subprojects {
hello << {println "- I depend on stserver"}
build << { println "source sets=$sourceSets.main.java.srcDirs" }
}
My child webserver build.gradle file:
sourceSets.main{
java.srcDirs = ['app']
}
build << { println "source sets=$sourceSets.main.java.srcDirs" }
hello << {println "- Do something specific xxxx"}
What is the deal here, is overriding build special or something? Overriding my own hello task worked fine and I thought overriding build would be just as simple?
The reason the behaviour seems different is because
buildtask already exists andhellodoes not (and not becausebuildis special).In gradle you cannot do this:
This will fail with the familiar error:
"Cannot add task ':hello' as a task with that name already exists.".Since
buildtask already exists, it’s illegal to have a secondtask build << { ... }. However, it will work forhellotask, because it does not exist, and thereforetask hello << { ... }is legal, as it’s the first declaration ofhellotask.If you replace your
task build << { ... }withbuild << { ... }, which just adds more behaviour to an existing task, it will “compile” fine.