首页 > 解决方案 > 为什么Gradle在使用dependsOn时总是按字母顺序执行任务依赖

问题描述

无法理解为什么依赖任务在使用时按字母顺序执行dependsOn。更让我困惑的是Gradle in Action一书 第 84 页In Gradle, the task execution order is not deterministic上的声明。

后来作者继续说It’s important to understand that Gradle doesn’t guarantee the order in which the dependencies of a task are executed.

如果执行顺序是不确定的,那么顺序不应该总是按字母顺序排列,即它应该是随机的。

如果有人可以对此有所了解,那就太好了。查找有关 gradle 任务执行顺序的问题,但我的问题有点不同,因为我想知道为什么作者称执行顺序为非确定性的,为什么他甚至继续说没有保证的顺序,而实际上是什么我通过尝试几个例子看到是完全相反的(即总是按字母顺序排列)。

编辑 1 开始

我的 gradle 脚本(build.gradle):

version = '0.1-SNAPSHOT'

task first{
    doLast{
        println "first"
    }
}
task second {
    doLast{
        println "second"
    }
}

task fourth {
    doLast{
        println "fourth"
    }
}

task wonder {
    doLast{
        println "Wonder"
    }
}

task thunder {
    doLast{
        println "thunder"
    }
}

task apple {
    doLast{
        println "apple"
    }
}

task printVersion(dependsOn: [second, first, fourth, wonder, thunder, apple]) {
    doLast{
        logger.quiet "Version: $version"
    }
}

task third{
    doLast{
        println "third"
    }
}

third.dependsOn('printVersion')    

以下是我每次尝试运行脚本时的顺序:

:apple SKIPPED
:first SKIPPED
:fourth SKIPPED
:second SKIPPED
:thunder SKIPPED
:wonder SKIPPED
:printVersion SKIPPED
:third SKIPPED 

编辑 1 结束

标签: gradlebuild.gradle

解决方案


推荐阅读