Hello, I'm writing a toolchain resolver plugin and...
# community-support
a
Hello, I'm writing a toolchain resolver plugin and I noticed the
JavaToolchainResolver
is a Gradle
BuildService
and I'm wondering if it is possible to inject additional dependencies via constructor and
@Inject
. I'm registering the dependencies via
settings.gradle.sharedServices
however it seems that the resolver class cannot determine those dependencies due to
Unable to determine constructor argument #1: missing parameter of type MyService
In my plugin::apply I'm doing the following
Copy code
val serviceRegistry = (target as SettingsInternal).services
val toolchainResolverRegistry = serviceRegistry.get(JavaToolchainResolverRegistry::class.java)
toolchainResolverRegistry.register(MyResolver::class.java)

target.gradle.sharedServices.registerIfAbsent("myService", MyService::class.java)
Also my resolver looks like this
Copy code
abstract class MyResolver
    @Inject
    constructor(
        val myService: MyService,
    ) : JavaToolchainResolver
I'm a little confused as I cant find documentation on how to wire dependencies for this type of BuildService
Actually I'm now registering the resolver on the toolchainResolverRegistry via https://docs.gradle.org/9.3.1/userguide/toolchain_plugins.html#register_the_resolver_in_a_plugin
But still can't wire the needed dependencies into it
Basically my question is how to do DI in a
JavaToolchainResolver
v
I'm wondering if it is possible to inject additional dependencies via constructor and
@Inject
.
Afair you cannot inject custom build services automatically anywhere except for tasks with a
@ServiceReference
annotated property, but you have to provide the constructor argument manually when doing the creation / registration call. But as the
JavaToolchainResolverRegistry#register
call does not allow to give constructor arguments or set parameters, I guess you might not be able to inject any dependency. Also
JavaToolchainResolver
explicitly implements
BuildService<None>
so has hard-coded that you cannot have parameters. But that's mainly wild guessing, as I did not play with own toolchain resolvers yet. Maybe you can have a setter and get the resolver service to then set the needed dependency before the resolver is used or something like that. 🤷‍♂️ What you should of course not do is using
SettingsInternal
, but instead inject an
JavaToolchainResolverRegistry
instance into your settings plugin like shown in the docs.
👍 1
😅 1