Parameterized task dependency


(Lasse Westh-Nielsen) #1

How can I use jettyRunWar in and out of daemon mode depending on the task I execute?

E.g., when I run my own task I want daemon mode: gradle acceptanceTest

But I also want to use gradle to just build and launch my web app, i.e. not in daemon mode: gradle jettyRunWar

In my build.gradle I have set daemon mode as a root property to tackle problem 1.

jettyRunWar.daemon = true

task acceptanceTest(dependsOn: jettyRunWar) << {

run some tests against my web app deployed in Jetty

}

But, I can’t seem to find a way to parametrise jettyRunWar as a dependency, run it via delegation, or execute a config target beforehand. So I can’t turn daemon mode off when I just want to build+run.

Help?!


(Lasse Westh-Nielsen) #2

Alright, this seems to work:

task jettyRunWarAsDaemon(dependsOn: jettyRunWar) << {}

gradle.taskGraph.whenReady {graph ->

if (graph.hasTask(jettyRunWarAsDaemon)) {

jettyRunWar.daemon = true

}

}

task acceptanceTest(dependsOn: jettyRunWarAsDaemon) << {

}

But it isn’t pretty…


(Peter Niederwieser) #3

That’s the best you can do today. If you need this more than once, you can abstract away the whenReady/hasTask logic into a method named, say, onlyIf, so that you can apply it like this:

onlyIf(acceptanceTest) {
  jettyRunWar.daemon = true
}