Hey all, I'm writing a convention plugin for my pr...
# kotlin-dsl
a
Hey all, I'm writing a convention plugin for my project where I want to have a common setup for configuring Gradle test suites although I want to be able to set dependencies on the buildscript, currently I'm doing the following
(code in comment)
This is working currently, but I do have two questions/concerns: • Do I always need to setup everything inside
project.afterEvaluate
? • Is there a more convenient type instead of Property for specifying a function? In order to avoid
.get()()
Thanks in advance ✌️
e.g. buildscript
Copy code
plugins {
    `common-plug`
}

test {
   dependencies {
      implementation...
   }
}
e.g. common-plug.gradle.kts
Copy code
interface TestExtension {
    val dependencies: Property<JvmComponentDependencies.() -> Unit>

    fun dependencies(block: JvmComponentDependencies.() -> Unit) {
        dependencies = block
    }
}

val extension = project.extensions.create<TestExtension>("test")

project.afterEvaluate {
    testing {
        suites {
            getByName<JvmTestSuite>("test") {
                //common config...
                dependencies {
                    extension.dependencies.get()()
                }
            }
        }
    }
}
e
https://docs.gradle.org/8.8-rc-1/release-notes.html#custom-dependencies-blocks the API is in Gradle 8.7, it just isn't documented until 8.8
a
Hmm but thats like "adding dependencies on the plugin" right?
How would i retrieve the dependencies to add on the testing?
e
no
Copy code
interface TestExtension {
    interface TestDependencies : GradleDependencies {
        val implementation: DependencyCollector
    }

    val dependencies: TestDependencies
    fun dependencies(action: Action<TestDependencies>) {
        action.execute(dependencies)
    }
}

project.configurations.getByName("testImplementation") {
    fromDependencyCollector(extension.dependencies.implementation)
}