Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle task build already exists issue

Tags:

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?

like image 547
Dean Hiller Avatar asked Jun 22 '12 13:06

Dean Hiller


People also ask

How do you solve the task wrapper not found on the project app?

Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Where is my build gradle?

The app level build. gradle file is located inside your project folder under app/build. gradle.

What does refresh gradle project do in Eclipse?

Project synchronization When modifying the build configuration, you can apply the changes by executing the Gradle > Refresh Gradle Project command from the context menu of the project node or the build script editor. Project synchronization even respects the customizations done in Gradle eclipse plugin configuration.


2 Answers

You aren't overriding the hello task, you are just adding more task actions. You can override a task with task foo(overwrite: true). I haven't come across a good reason to override the build task; there are probably better ways to achieve what you want.

like image 95
Peter Niederwieser Avatar answered Oct 26 '22 15:10

Peter Niederwieser


What is the deal here, overriding build is 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 build task already exists and hello does not (and not because build is special).

In gradle you cannot do this:

task hello << { print "hello" }
task hello << { print "hello again" }

This will fail with the familiar error: "Cannot add task ':hello' as a task with that name already exists.".

Since build task already exists, it's illegal to have a second task build << { ... }. However, it will work for hello task, because it does not exist, and therefore task hello << { ... } is legal, as it's the first declaration of hello task.

If you replace your task build << { ... } with build << { ... }, which just adds more behaviour to an existing task, it will "compile" fine.

like image 39
rodion Avatar answered Oct 26 '22 13:10

rodion