Skip to content

Instantly share code, notes, and snippets.

@ghale
Last active December 21, 2023 13:41
Show Gist options
  • Save ghale/907407a3ccc3ef497d2ce10ce4474db8 to your computer and use it in GitHub Desktop.
Save ghale/907407a3ccc3ef497d2ce10ce4474db8 to your computer and use it in GitHub Desktop.
Build Service for restricting only one test task to execute at a time
import org.gradle.api.services.*
subprojects {
apply plugin: OneTestTaskOnly
}
class OneTestTaskOnly implements Plugin<Project> {
void apply(Project project) {
// Register a service to constrain parallelism of test tasks to 1
Provider<SharedResource> testLimitingService = project.gradle.sharedServices.registerIfAbsent("testLimitingService", SharedResource) { spec ->
maxParallelUsages.set(1)
}
project.tasks.withType(Test).configureEach {
// Tell gradle that test tasks require the testLimitingService to execute
usesService(testLimitingService)
}
}
}
// Since we are only constraining task parallelism, our service implementation is pretty bare
abstract class SharedResource implements BuildService<BuildServiceParameters.None> {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment