Correct way to prevent two or more tasks from running in parallel?

My problem is that I have multiple subprojects that I want to be built in parallel, however each project has multiple tasks that cannot be run in parallel.

For Example:
ProjectA has a compileWindows and compileLinux task. These cannot be run in parallel.
ProjectB has a compileWindows and compileLinux task. These cannot be run in parallel.

I want ProjectA to be able to be built in parallel to ProjectB - so :ProjectA:compileWindows should be allowed to run in parallel to :ProjectB:compileWindows - but :ProjectA:compileWindows should not be able to run in parallel to :ProjectA:compileLinux

The best way I can think of to do this right now, based on reading the Task API documentation is to use Task.mustRunAfter(...) for the compile tasks within each project.

  1. Will using mustRunAfter(...) like this force both tasks to run? There are occasions where I may want compileWindows to run but not compileLinux and vice versa.
  2. Is there any other way I can get this behavior of preventing certain tasks from running in parallel? I find the mustRunAfter(...) approach odd because technically these tasks within the project do not need to be ordered in any way, they simply cannot run alongside one another.

No, mustRunAfter means if both tasks are run, then the on must run after the other, if only one runs, only one runs.

But I agree that it is not the right use-case.

Luckily there is a different way that does exactly what you want.

Define a shared build service with max concurrency 1 and then declare all tasks that are mutually exclusive to use that service, then always only one of those tasks can run.

For this purpose your shared build service does not need to have any logic at all.

1 Like