tim
09/28/2021, 12:45 PM./gradlew tasks
locally:
* What went wrong:
Failed to apply plugin class 'Build_gradle$FooPlugin'.
> Could not create plugin of type 'FooPlugin'.
> The constructor for type Build_gradle.FooPlugin should be annotated with @Inject.
tim
09/28/2021, 12:45 PMabstract class FooExtension {
abstract val bar: Property<String>
init {
println("Hi from FooExt")
}
}
class FooPlugin : Plugin<Project> {
override fun apply(target: Project) {
val extension = target.extensions.create<FooExtension>("foo")
project.task("hello") {
doLast {
println(extension.bar.get())
}
}
}
}
apply<FooPlugin>()
extensions.getByType<FooExtension>().bar.set("foo")
Vampire
09/28/2021, 1:23 PM.gradle.kts
file.
Because of that the class FooPlugin
is an inner class of the class Build_gradle
and thus has the instance of the enclosing class as argument to the constructor.
And as now the constructor has a parameter it would need to get the value injected which of course would not work even if the annotation were present.tim
09/28/2021, 1:31 PMbar
isn't initalised .. so i could add something to the contstructor to initialise it init { bar.set("baz") }
which should address the issuetim
09/28/2021, 1:32 PMVampire
09/28/2021, 1:32 PMVampire
09/28/2021, 1:32 PMbar
IS initialized, it gets implemented and initilaized by GradleVampire
09/28/2021, 1:32 PMVampire
09/28/2021, 1:33 PMFooPlugin
into its own file in an included build or buildSrc
and it should work as it is then no longer an inner class that needs the instance of the outer class in the constructorVampire
09/28/2021, 1:34 PMtim
09/28/2021, 1:34 PMtim
09/28/2021, 1:56 PMIt is very uncommon to have a plugin class within a build script, because then you could also just do the actionAgreed .. but its from the official gradle docs 🤷
tim
09/28/2021, 2:12 PMVampire
09/28/2021, 2:22 PMVampire
09/28/2021, 2:23 PMproject
to target
, but you still do project.task
.
So project
is the instance of the enclosing script and that's why there is a connection that cannot be initializedVampire
09/28/2021, 2:24 PMproject.task
to target.task
, it works.tim
09/28/2021, 2:24 PMVampire
09/28/2021, 2:25 PM