Hi, I'm trying out some small things with kotlin &...
# gradle
a
Hi, I'm trying out some small things with kotlin & gradle kotlin dsl and also came across https://github.com/gradle/kotlin-dsl/blob/master/doc/getting-started/Configuring-Plugins.md and I was wondering why using the
plugins{}
block is heavily recommended, isn't using
apply<>()
and
configure<>
also type-safe? The reason I'm sort of gravitating towards the
apply
and
configure
is because I feel like you can group it better together then (for e.g. make a `fun setupApplication() { /*appply and configure*/}`; but I'm very new to messing around with gradle and especially kotlin so maybe I'm misguided here?
e
You’re right,
the<T>()
and
configure<T> {}
are type safe. The type to use may be hard to discover. When you use the
plugins {}
block the Kotlin DSL provides easy to discover Kotlin extensions, saving you some burden. It also enables type safe configurations accessors for dependencies declaration that you have to replace with string references when using
apply
.
a
With the
plugins{}
block you can't provide any context though, for example I'm using the
idea
plugin for a workaround (https://youtrack.jetbrains.com/issue/IDEA-188436) so with apply and configure I can do
Copy code
/**
 * Workaround for [issue explained here](<https://youtrack.jetbrains.com/issue/IDEA-188436>)
 */
fun resolveIdeaTestDependenciesWorkAround() {
    apply<IdeaPlugin>()
    @Suppress("UNUSED_VARIABLE") //explicit val and delegate required to configure task
    val ideaModule by tasks.getting(GenerateIdeaModule::class) {
        module.scopes["COMPILE"]!!["plus"]!! += configurations.testCompile
    }
}
oh that didn't format as I would've hoped; anyway that function is called within a
configureBothMainAndTestSourceSetsUnderSrc
function, and I personally like that composition&grouping better. Because afaict you can only have 1
plugins{}
block
e
both styles work well, yes