thinkingmonster
thinkingmonster

Reputation: 5453

task declaration not working in gradle

I have gradle file with below code which is failing.

task wakeup
task dressup
task playMusic
task goRunning

//dependsOn(task)

wakeup <<{  
    println("I am awake,i need to go for a run")
}

dressup(dependsOn: wakeup)<<{
    println("I am ready with my track suit")
}

playMusic(dependsOn: dressup)<<{
    println("I have played track 7")
}

goRunning(dependsOn: playMusic)<<{
    println("I am running")
}

ERROR:-

C:\Users\akathaku\Desktop\gradlelearning>gradle -q -b taskmethods.gradle goRunning

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\akathaku\Desktop\gradlelearning\taskmethods.gradle' line: 14

* What went wrong:
A problem occurred evaluating root project 'gradlelearning'.
> Could not find method dressup() for arguments [{dependsOn=task ':wakeup'}] on root project 'gradlelearning'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

But if i change the code to:-

//dependsOn(task)

task wakeup <<{ 
    println("I am awake,i need to go for a run")
}

task dressup(dependsOn: wakeup)<<{
    println("I am ready with my track suit")
}

task playMusic(dependsOn: dressup)<<{
    println("I have played track 7")
}

task goRunning(dependsOn: playMusic)<<{
    println("I am running")
}

Its running perfectly. Normaly declaring a task and using it later works.But with dependsOn method this is failing.Why?

Upvotes: 0

Views: 45

Answers (1)

Ori Lentz
Ori Lentz

Reputation: 3688

Your problem isn't the dependsOn method, as clearly it works in your second example.

The problem is that you're missing the task declaration when trying to define the task body, so gradle interprets the groovy code as a method call. Meaning, when you write:

dressup(dependsOn: wakeup)<<{
    println("I am ready with my track suit")
}

Gradle doesn't recognize it as a task, but rather sees the dressup(dependsOn: wakeup) portion as you trying to call a method named dressup with the parameter {dependsOn: wakeup}. But no such method exists, and you get the error.

Which is why you always need to tell gradle that it's a task, i.e.:

task dressup(dependsOn: wakeup)<<{
    println("I am ready with my track suit")
}

As per your second (and successful) example.

Upvotes: 1

Related Questions