Gradle:排除(不跳过)最新的任务

Jac*_*ach 5 gradle

我正在寻找一种方法来避免运行最新任务的依赖项(同时仍然运行任何其他任务的依赖项,而不是最新任务)。

采用以下结构:

task stopService {}
task startService {}
task doUpdateA { // change something about the service }
task doUpdateB { // change something else about the service }

task updateA {
    inputs.files <some inputs>
    output.upToDateWhen { true }
    // i.e. run whenever inputs changed from previous run

    dependsOn stopService
    dependsOn doUpdateA
    dependsOn startService

    // ensure the service is stopped while it's being modified
    doUpdateA.mustRunAfter stopService
    startService.mustRunAfter doUpdateA
}


task updateB {
    inputs.files <some inputs>
    output.upToDateWhen { true }
    // i.e. run whenever inputs changed from previous run

    dependsOn stopService
    dependsOn doUpdateB
    dependsOn startService

    // ensure the service is stopped while it's being modified
    doUpdateB.mustRunAfter stopService
    startService.mustRunAfter doUpdateB
}

task updateAll {
    dependsOn updateA
    dependsOn updateB
}
Run Code Online (Sandbox Code Playgroud)

所需的执行流程./gradlew updateAll

  • 如果 updateA 和 updateB 都是最新的,则什么都不做
  • 如果其中之一不是最新的, stopService -> doUpdateX -> startService
  • 如果它们都不是最新的,stopService -> doUpdateA -> doUpdateB -> startService(或 B 在 A 之前)。

这是可能的,也许是通过挂钩到任务执行图并在所有任务上手动运行 upToDate,然后在它们是最新的情况下将它们排除在外?假设没有任务使另一个任务过时。

---- 半解决方案:

如果我忽略第三个要求,这可以通过执行以下操作来解决: task updateB { input.files output.upToDateWhen { true } // 即每当输入从上次运行发生变化时运行

    doLast {
        stopService.execute()
        doUpdateB.execute()
        startService.execute()
    }
}
Run Code Online (Sandbox Code Playgroud)

但由于几个原因,这是次优的 - 不会运行 stop/startService 的依赖项/终结器(我认为?),每次更新都会导致单独的停止/启动(使构建时间更长)等。

如果您忽略要求 1,则类似于:

task updateAll {
    inputs.files <some inputs>
    output.upToDateWhen { true }
    // i.e. run whenever inputs changed from previous run

    dependsOn stopService
    dependsOn doUpdateA
    dependsOn doUpdateB
    dependsOn startService

    doUpdateB.mustRunAfter stopService
    startService.mustRunAfter doUpdateB
    doUpdateA.mustRunAfter stopService
    startService.mustRunAfter doUpdateA
}
Run Code Online (Sandbox Code Playgroud)

将最新检查移至 doUpdateX 工作,但由于此边距太短而无法包含的原因,这又是次优的。

在此先感谢您的任何建议,