Start/stop server puzzle

I am writing a Gradle plugin for a custom type of project. The goal of the plugin is to run some tests which are each described in the build file using a DSL.

Constraints of my project:

  • Before any test is run, I need to start a server which will gather the test reports (task “‘reportServer.start’”).

  • After all tests are run, I need to stop the server (task “‘reportServer.stop’”).

  • I wish to allow the user to launch all the tests described in the DSL with the command “‘gradle runTests’”.

  • I wish to allow the user to launch any list of tests chosen individually with a command like “‘gradle test1.run test2.run test3.run’”.

Question: How to plug the tasks ‘reportServer.start’ and ‘reportServer.stop’ so that they are executed at the right time?

For the task ‘reportServer.start’, it’s easy: each test task can depend on it. For the task ‘reportServer.stop’, I didn’t find any satisfying solution and that’s the problem.

I partially solved the problem by having the user specifying a task about the server in the command line:

gradle reportServer test3.run

The task reportServer depends on reserver.stop and reportServer.start, then with an order defined between the tasks, I achieved what I wanted. However, my solution is not complete as the user still have to call the task reportServer in the command line.

I found a way to access the list of the tasks from the command line (was something like “project.gradle.startCommand”), but I found no way to dynamically add tasks to it. IMHO, this is a feature which is needed.

For stopping, you need ‘finalizedBy’ from Gradle 1.7 (out in a few days). There’s no elegant solution for earlier versions.

You can read about it here…

http://www.gradle.org/docs/1.7-rc-1/release-notes#finalizer-tasks

Thank you.

The finalizedBy is providing an alternative to being able to programmatically add tasks in the command line. That’s also more clean because it still let the plugins know that the tasks in the command line are indeed the ones specified by the user and are not added by some plugins somewhere.