我有以下代码失败的gradle文件。
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")
}
错误: -
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.
但如果我将代码更改为: -
//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")
}
它运行得很好。 Normaly声明一项任务并在以后使用它。但是使用dependsOn方法这是失败的。为什么?
答案 0 :(得分:1)
您的问题不是dependsOn method
,显然它适用于您的第二个示例。
问题是你在尝试定义任务主体时缺少task
声明,因此gradle将groovy代码解释为方法调用。这意味着,当你写:
dressup(dependsOn: wakeup)<<{
println("I am ready with my track suit")
}
Gradle不会将其识别为任务,而是在您尝试使用参数dressup(dependsOn: wakeup)
调用名为dressup
的方法时看到{dependsOn: wakeup}
部分。但是没有这样的方法,你得到错误。
这就是为什么你总是需要告诉gradle这是一项任务,即:
task dressup(dependsOn: wakeup)<<{
println("I am ready with my track suit")
}
根据你的第二个(也是成功的)例子。