本文介绍了如果另一个不是最新的,则仅运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Gradle中进行功能测试时自动添加serverRun任务,所以我添加了一个依赖项:

I want to automatically add a serverRun task when doing functional tests in Gradle, so I add a dependency :

funcTestTask.dependsOn(serverRun)

无论funcTestTask是否运行,都会导致任务运行

Which results in the task running whether or not the funcTestTask even runs

:compile
:serverRun
:funcTestTask (and associate compile tasks... etc)
:serverStop

OR

:compile UP-TO-DATE
:serverRun <-- unnecessary
:funcTestTask UP-TO-DATE
:serverStop

启动服务器的成本非常高,我只希望在功能测试不是 UP-TO-DATE时启动,我想这样做或做某事:

The cost of starting the server is pretty high and I only want it to start if the functionalTest isn't UP-TO-DATE, I'd like to do or something :

if(!funcTestTask.isUpToDate) {
    funcTestTask.dependsOn(serverRun)
}

所以我知道在确定所有输入/输出之前,我不知道funcTestTask的最新状态,但是我可以继承它的uptoDate检查器吗?

So I know I can't know the up-to-date status of funcTestTask until all it's inputs/outputs are decided BUT can I inherit it's uptoDate checker?

serverRun.outputs.upToDateWhen(funcTestTask.upToDate)

另一种方法是在FuncTest中首先执行" ServerRun,我认为这通常是不受欢迎的?

The alternative is to "doFirst" the ServerRun in the FuncTest, which I believe is generally frowned upon?

funcTestTask.doFirst { serverRun.execute() }

是否可以有条件地先执行一个任务?

Is there a way to conditionally run a task before another?

更新1
尝试过的设置输入/输出相同

UPDATE 1
Tried settings inputs/outputs the same

serverRun.inputs.files(funcTestTask.inputs.files)
serverRun.outputs.files(funcTestTask.outputs.files)

这似乎在重新编译时重新运行服务器(良好),在成功进行未更改的功能测试后跳过重新运行(也很好),但是在如下所示的失败测试之后不会重新运行测试

and this seems to rerun the server on recompiles (good), skips reruns after successful unchanged functional tests (also good), but wont rerun tests after a failed test like the following

:compile
:serverRun
:funcTestTask FAILED

then

:compile UP-TO-DATE
:serverRun UP-TO-DATE <-- wrong!
:funcTestTask FAILED

推荐答案

我最终写了一个失败文件",并在serverRun任务中输入了该信息:

I ended up writing to a 'failure file' and making that an input on the serverRun task:

File serverTrigger = project.file("${buildDir}/trigger")

project.gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
  // make the serverRun task have the same inputs/outputs + extra trigger
  serverRun.inputs.files(funcTestTask.inputs.files, serverTrigger)
  serverRun.outputs.files(funcTestTask.outputs.files)
}

project.gradle.taskGraph.afterTask { Task task, TaskState state ->
  if (task.name == "funcTestTask" && state.failure) {
    serverRun.trigger << new Date()
  }
}

在Gradle论坛上,从回答我的问题得到的信息: http://forums.gradle.org/gradle/topics/how-can-i-start-a-server-conditionally-be-a-functionaltestrun

With information from an answer to my question on the Gradle forums :http://forums.gradle.org/gradle/topics/how-can-i-start-a-server-conditionally-before-a-functionaltestrun

这篇关于如果另一个不是最新的,则仅运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:23