how do I retrieve a build service in an extension....
# community-support
c
how do I retrieve a build service in an extension. I have access to
Project
, this is not for a task. It should already be registered
m
My personnal guess would be that you can't. extensions have no lifecycle so you can't really bind a build service to it
I'm curious if anyone has a definitive answer
c
in theory you should be able to retrieve it from
project
, I'm not really talking about binding it to a property, but rather using it in a method
👀 1
m
If you can retrieve it from project then keep a reference to the project from your extension?
c
I do, but I can't figure out how to requiest the build service from project
it tells me how to register it, and bind it to a task
but not how to retrieve one
m
Looks like you may be able to do
registerIfAbsent("...", Foo::class).get()
?
So you always register it but you might get it as a side effect of registering it?
c
feels, weird, and makes me wonder what happens if you don't pass the parameters again
m
Yea the name probably needs to match 🤷
c
right, I'm talking about the 3rd argument which sets the parameters on the service
if you have them
fortunately having the name isn't a problem because I'm just using the canonical name of the class... but still, this is super unintuitive I guess I could TIAS and hope it works.
or I can just pass it into the extension..
v
You should be able to get the already registered provider from the registry iirc
c
yeah, but how?
Copy code
this.project.getGradle().getSharedServices().getRegistrations().withType(GitService.class)
that's not it, because that's expecting the registration type
not the service
v
by name
c
like this?
Copy code
this.project.getGradle().getSharedServices().getRegistrations().named(GitService.class.getCanonicalName()).get().getService()
which means I have to cast the type?
v
yes
something like that