如果有什么不是最新的,如何询问gradle

时间:2017-02-06 09:25:56

标签: gradle

有没有办法问gradle是否有什么不是最新的并且需要构建(当时没有实际构建它)?

1 个答案:

答案 0 :(得分:4)

这是不可能的。最新检查发生在执行阶段,就在doFirst运行之前。它被计算并且不存储该值。此外,最新检查可能取决于先前的任务输出。因此,要解决任务的最新状态,您需要执行其所有依赖项(dependsOn任务)。因此,您所要求的理论上只适用于第一个不是最新的任务。

要了解生命周期的工作原理,这里有一个简单的例子:

task hello {
    println "CONFIG1"
    outputs.upToDateWhen {
        println "UPTODATE"
        return false
    }
    println "CONFIG2"
    doFirst {
        println "DOFIRST"
    }
    doLast {
        println "DOLAST"
    }
}

如果使用--debug标志执行此操作,您将首先看到:

08:05:17.294 [DEBUG] [org.gradle.model.internal.registry.DefaultModelRegistry] Project : - Registering model element 'tasks.hello' (hidden = false)
08:05:17.302 [QUIET] [system.out] CONFIG1
08:05:17.329 [QUIET] [system.out] CONFIG2
08:05:17.333 [DEBUG] [org.gradle.configuration.project.BuildScriptProcessor] Timing: Running the build script took 0.764 secs

配置阶段设置最新检查。然后Gradle计算任务图:

08:16:26.212 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskGraphExecuter] Timing: Creating the DAG took 0.007 secs

然后才执行任务:

08:05:17.430 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :hello (Thread[main,5,main]) started.
08:05:17.431 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] :hello
08:05:17.432 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Starting to execute task ':hello'
08:05:17.461 [QUIET] [system.out] UPTODATE
08:05:17.465 [INFO] [org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter] Putting task artifact state for task ':hello' into context took 0.032 secs.
08:05:17.465 [DEBUG] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Determining if task ':hello' is up-to-date
08:05:17.465 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Executing task ':hello' (up-to-date check took 0.0 secs) due to:
  Task.upToDateWhen is false.
08:05:17.466 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':hello'.
08:05:17.467 [QUIET] [system.out] DOFIRST
08:05:17.467 [QUIET] [system.out] DOLAST

只有在构建任务树并按顺序执行任务时才会进行最新检查。

由于此检查没有存储任何值,因此无法在不执行所依赖的所有任务的情况下确定任务的最新状态。