I want to create a plugin in Kotlin that has behavior to the maven-publish
plugin (let’s call it foo
).
The main feature is to produce a set of tasks which are the product of two collections:
foo {
targetDir = layout.projectDirectory.dir("output-dir")
dim1 {
create<Dim1>("G") {
x = 1
y = "dog"
}
create<Dim1>("H") {
x = 2
y = "cat"
}
}
dim2 {
create<Dim2>("X") {
z = 1
}
create<Dim2>("Y") {
z = 2
}
}
}
The goal is for this to cause the foo
plugin to generate 4 tasks: taskGX, taskGY, taskHX, and task HY.
I wrote a plugin using the following code fragments:
Dim1.kt
abstract class Dim1 @Inject constructor(val name: String) {
abstract val x: Property<Int>
abstract val y: Property<String>
}
Dim2.kt
abstract class Dim2 @Inject constructor(val name: String) {
abstract val z: Property<Int>
}
override fun apply(project: Project): Unit = project.run {
val extension = project.extensions.create<FooExtension>("foo")
with (extension as ExtensionAware) {
val dim1 = project.extensions.create<Dim1>("dim1")
val dim2 = project.extensions.create<Dim2>("dim2")
tasks {
register<FooTask>("task{dim1.name}{dim2.name}") {
...
}
}
}
}
This works but dim1
and dim2
are not containers.
I want to create two containers of data objects (one for Dim1 and one for Dim2)
as described in Implementing Binary Plugins
My problem (I think) is that the example shows a Java example and I want to use Kotlin.
I believe I need to somehow replace the project.extensions.create<Dim1>("dim1")
with something like project.extensions().add("dim1", dim1Container)
.
override fun apply(project: Project): Unit = project.run {
val extension = project.extensions.create<FooExtension>("foo")
with (extension as ExtensionAware) {
val dim1Container: NamedDomainObjectContainer<Dim1> =
project.container(Dim1::class)
project.extensions.add("dim1", dim1Container)
val dim2Container: NamedDomainObjectContainer<Dim2> =
project.container(Dim2::class)
project.extensions.add("dim2", dim2Container)
tasks {
// product over the two containers
register<FooTask>("task{dim1.name}{dim2.name}") {
...
}
}
}
}
}
I get this error
No type arguments expected for fun create(name: String!, configureClosure: Closure<(raw) Any!>!): T!
That is where I get lost.
Can I get some advice?