Created
August 19, 2020 21:48
-
-
Save bamboo/b4c9678fc44aebc623fe849351ea95d3 to your computer and use it in GitHub Desktop.
BuildService + projectsEvaluated callback example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.tooling.events.FinishEvent | |
import javax.inject.Inject | |
abstract class TraceService : BuildService<TraceService.Parameters>, org.gradle.tooling.events.OperationCompletionListener { | |
private | |
var projectsEvaluated = false | |
interface Parameters : BuildServiceParameters { | |
} | |
fun onProjectsEvaluated() { | |
println("onProjectsEvaluated()") | |
projectsEvaluated = true | |
} | |
override fun onFinish(event: FinishEvent) { | |
if (!projectsEvaluated) { | |
println("from the cache!") | |
} | |
println("onFinish($event)") | |
} | |
} | |
open class TraceServicePlugin @Inject constructor( | |
private val buildEventsListenerRegistry: BuildEventsListenerRegistry | |
) : Plugin<Settings> { | |
override fun apply(target: Settings) { | |
target.gradle.run { | |
val service = sharedServices.registerIfAbsent("traceService", TraceService::class) { | |
parameters { | |
// configure service here | |
} | |
} | |
buildEventsListenerRegistry.onTaskCompletion(service) | |
projectsEvaluated { | |
service.get().onProjectsEvaluated() | |
} | |
} | |
} | |
} | |
apply<TraceServicePlugin>() |
@bitspittle Hey, I did move on by not supporting configuration cache in my plugins 😆
Your solution seems reasonable but I remember trying it in the past and somehow it wouldn't work for my case or perhaps I must have missed something important.
You might wanna check this thread here gradle/gradle#14860 (comment) Where I basically asked the same thing and received a similar response.
That's a great thread, thanks for sharing. Lots of people are struggling with this :)
Luckily that approach is working for me so far. I am uncomfortable that I'm checking an interface match, since Gradle could possibly invent some different failure-ish type in the future and my code will silently stop working, which will be a pain to debug.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you a ton for this gist. I came here after the Gradle docs dropped the ball and basically said, "If you needed our method to do X, well it's deprecated now. A migration example? Nah." (see also: https://docs.gradle.org/current/userguide/build_services.html#operation_listener)
@ThanosFisherman I'm here two months later, so you may have moved on, but the way I'm solving what you're trying to do is by using instance checks against
FailureResult
: