Only run task if another isn't UP

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

funcTestTask.dependsOn(serverRun)

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

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)
}

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)

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?

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

Having faced the same problem I found a very clean solution. In my case I want an eclipse project setup to be generated when the build is run, but only at the times when a new jar is generated. No project setup should be executed when the jar is up to date. Here is how one can accomplish that:

tasks.eclipse {
  onlyIf {
    !jar.state.upToDate
  }
}

build {
  dependsOn tasks.eclipse
}

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 { TaskExectuionGraph 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()
  }
}

with info 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


由于该任务是您试图控制的任务的依赖任务,因此您可以尝试:

tasks {
    onlyIf {
        dependsOnTaskDidWork()
    }
}
链接地址: http://www.djcxy.com/p/24292.html

上一篇: 如何在没有控制台访问的情况下处理主机上的yii2迁移?

下一篇: 只有在别人不在时才运行任务