Define task order without dependency in Gradle?

Question

Can I somehow make sure that the task will be completed before / after another task, when both of them should be completed? I do not want to add a dependency between them that always exists.

Background

I have two tasks, one of which starts the server, and the other - tests. The task that starts this server is to start it in a new process, so I can start gradle startServer runServerTests . Since it takes some time to start the server, it should be possible to start the server with one gradle process and then run the tests several times using another gradle process.

Now I'm trying to create one task that does everything that our CI environment does, which includes starting the server and running server tests. Obviously, I want to make sure that the server is running before running the tests, but so far I'm out of luck.

Attempts

My first attempt is below, but this does not work, since the order of certain dependencies is not guaranteed :

 task doItAll(dependsOn: [startServer, runServerTests]) { ... } 

My second attempt, calling tasks in actions does not work and is not supported :

 task doItAll() << { tasks.startServer.execute() tasks.runServerTests.execute() } 

The solutions are on the roadmap ( GRADLE-294 , Initializer / Finalizer ), but that does not help me now.

+6
source share
5 answers

Most likely, this will not help you at the moment, but I recently submitted a transfer request in this area, and it was planned that it should turn into 1.6 (they currently release 1.5, and PR do not do this in this release) - see discussion here . Itโ€™s best to wait for the pull request to be merged into master after release 1.5, and then grab the first available nightly build from here .

EDIT

Gradle 1.6 was released some time ago, and now you can simply use mustRunAfter to achieve this. For more information, see the section on organizing tasks in the Gradle manual .

+6
source

http://issues.gradle.org/browse/GRADLE-294 was allowed on May 1 (Gradle 1.6) and now gives you a way to describe that task B should run before A if both are present when using the mustRunAfter () relationship between tasks.

Therefore, I think your question is fully answered.

+1
source

gradle Improvements to the finalizedBy and mustRunAfter tasks to solve these problems. However, as with the OP, I needed to change the dependencies and finalization based on the requested tasks, and not statically.

I wanted gradle integrationTest to run , runTests and finalize by shutting down . I also wanted to run runTests and shutdown to be able to run independently - without any dependencies or completion.

You can statically create wrapper tasks that could express this, however it did not scale as the number and complexity of startup and shutdown tasks increased, and relied on those who added more tasks to add and maintain the necessary wrappers.

I found it more elegant to implement integrationTests typically tasks.

 tasks.addRule('integrationTest: Full execution of integration tests with setup, startup, shutdown and clean up') { String taskName -> if (taskName.equals('integrationTest')) { task(dependsOn: [runTests], taskName) // Add ordering and finalization for integration test runs runTests.dependsOn startup startup.finalizedBy shutdown ... // and many other setup and shutdown tasks you want in the task DAG when running an integration test } } 
+1
source

You can solve your order problem by explicitly declaring one task dependency:

 runServerTests.dependsOn startServer task doItAll(dependsOn: runServerTests) << { // do something } 

There it works in a pipeline that addresses your specific use case for raising another component, for example. Servlet container for testing purposes.

0
source

I had the same challenge as me - this is what I did to solve it (full version):

https://caffeineinduced.wordpress.com/2015/01/25/run-a-list-of-gradle-tasks-in-specific-order/

TL; DR; version:

 //--- build aliases : define a synonym here if you want a shortcut to run multiple targets def buildAliases = [ 'all' : ['clean', 'assemble', 'runProvisioner', 'stopTomcat', 'installTomcat', 'deployToTomcat', 'startTomcat'], 'rebuild' : ['clean', 'assemble'] ] def expandedTaskList = [] gradle.startParameter.taskNames.each { expandedTaskList << (buildAliases[it] ? buildAliases[it] : it) } gradle.startParameter.taskNames = expandedTaskList.flatten() println "\n\n\texpanded task list: ${gradle.startParameter.taskNames }\n\n" 
0
source

All Articles