Gradle任务执行

时间:2014-10-04 15:53:33

标签: groovy gradle

以下gradle构建脚本

task hello << {
            println "task hello"
}

hello << {
   println "th"
}

产生输出:

task hello
th

但是一个人的订单倒退导致了错误:

* What went wrong:
A problem occurred evaluating root project 
> Could not find property 'hello' on root project

那是什么错误?你能用Groovy的观点解释一下吗?

1 个答案:

答案 0 :(得分:1)

可以通过以下方式在Java伪代码中翻译代码:

Task task = project.addTask("hello");
task.doLast(() -> println("hello"));

task = project.getTaskByName("hello"); // works fine, because a task "hello" has bee added before
task.doLast(() -> println("th"));

如果你切换这两个操作,你得到

Task task = project.getTaskByName("hello"); // doesn't work, because no task "hello" has been defined before
task.doLast(() -> println("th"));

task = project.addTask("hello");
task.doLast(() -> println("hello"));

显然,既然您正在尝试修改尚未定义的任务,那么该功能无法正常工作。