Skip to content

Instantly share code, notes, and snippets.

@jonnycaley
Created June 18, 2022 15:44
Show Gist options
  • Save jonnycaley/4c42200e197b548730cbd9257fda5ffc to your computer and use it in GitHub Desktop.
Save jonnycaley/4c42200e197b548730cbd9257fda5ffc to your computer and use it in GitHub Desktop.
BuildTimeLoggerPlugin - register build reporter service
class BuildTimeLoggerPlugin : Plugin<Project> {
override fun apply(project: Project) {
val gradle = project.gradle
val buildDurationService = registerBuildDurationService(gradle)
val buildTaskService = registerBuildTaskService(gradle)
registerBuildReporterService(gradle, buildDurationService, buildTaskService)
}
private fun registerBuildReporterService(
gradle: Gradle,
buildDurationService: Provider<BuildDurationService>,
buildTaskService: Provider<BuildTaskService>
): Provider<BuildReporterService> {
val registry = gradle.serviceRegistry()[BuildEventListenerRegistryInternal::class.java]
val buildReporterService = gradle.sharedServices.registerIfAbsent("build-reporter-service", BuildReporterService::class.java) { service ->
service.parameters.getBuildDurationServiceProvider().set(buildDurationService)
service.parameters.getBuildTaskServiceProvider().set(buildTaskService)
}
registry.onOperationCompletion(buildReporterService) // gives gradle a reason to instantiate the build service and call onClose at the end of the build
return buildReporterService
}
private fun registerBuildDurationService(gradle: Gradle) { ... }
private fun registerBuildTaskService(gradle: Gradle) { ... }
}
fun Gradle.serviceRegistry(): ServiceRegistry = (this as DefaultGradle).services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment