在我的build.gradle中,我有以下几行代码:
task aExecutionTask << {
println 'hello says aExecutionTask, via the execution phase'
}
task aConfigurationTask{
println 'hello says aConfigurationTask, via the configuration phase'
doLast{
println 'im last to get done'
}
}
当我使用
从命令行运行aExecutionTask时 ./gradlew aExecutionTask
输出如下:
hello says aConfigurationTask, via the configuration phase
:app:aExecutionTask
hello says aExecutionTask, via the execution phase
为什么不打印出我的doLast声明?它清楚地执行了任务“aConfigurationTask”但它没有运行它的doLast。那么我是否理解aConfigurationTask已配置但未执行?是那个概念吗?
答案 0 :(得分:1)
在配置阶段,代码为<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/res_pressed" />
<corners android:topLeft="10dp" android:bottomLeft="10dp" />
</shape>
</item>
<item
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
android:left="15dp">
<shape android:shape="rectangle">
<solid android:color="@color/res_default" />
<corners android:topRight="10dp" android:bottomRight="10dp" />
</shape>
</item>
</layer-list>
。因此,在配置任务时,任务实际上并未执行。执行任务后将调用evaluated
块。使用您的代码可以通过不提供命令来看到,这是任务的配置。然后调用任务,即执行任务,并调用任何doLast
个闭包。
doLast
如果您希望在$ gradle
Configuration on demand is an incubating feature.
hello says aConfigurationTask, via the configuration phase
:help
Welcome to Gradle 2.11.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
Total time: 0.629 secs
$ gradle aConfigurationTask
Configuration on demand is an incubating feature.
hello says aConfigurationTask, via the configuration phase
:aConfigurationTask
im last to get done
BUILD SUCCESSFUL
Total time: 0.603 secs
之前执行aConfigurationTask
任务,则需要aExecutionTask
依赖于aExecutionTask
aConfigurationTask
执行时,将调用task aExecutionTask << {
println 'hello says aExecutionTask, via the execution phase'
}
task aConfigurationTask {
println 'hello says aConfigurationTask, via the configuration phase'
doLast {
println 'im last to get done'
}
}
aExecutionTask.dependsOn aConfigurationTask
块。这是一个例子:
doLast