https://kotlinlang.org logo
Title
x

Xavier F. Gouchet

09/27/2019, 4:00 PM
Hello devs, I'm migrating all my gradle scripts from groovy to kotlin, and finding myself in a dead end it seems. Most of it is working well, but I used to have a single script to configure specific plugins, eg
detekt.gradle
that would apply the same detekt config to all projects needing it. Now when I try to do that, I can still use the
apply(from("path/to/detekt.settings.gradle.kts"))
, but apparently anything I put in the settings script doesn't have the usual imports, and cannot resolve any of the extensions.
My
detekt.settings.gradle.kts
have this inside :
detekt {
    version = "1.0.1"

    input = files("$projectDir/src/main/kotlin")
    config = files("$project.rootDir/script/config/detekt.yml")
    reports {
        xml {
            enabled = true
            destination = file("build/reports/detekt.xml")
        }
    }
}
And if I copy paste it directly in a module's script, it works, but through the apply from it doesn't, and I get a > Unresolved reference: detekt
x

Xavier F. Gouchet

09/28/2019, 3:34 PM
That does make sense to use
buildSrc
for that
Thanks @tapchicoma
Well unfortunately, to be able to configur especific plugins, I need to be able tu use the dsl generated by them, which is not possible at compile time from the
buildSrc
folder.
Or there is a trick to do that
t

tapchicoma

09/28/2019, 3:40 PM
I think you need to add 3rd party plugin as a dependency to your
buildSrc
x

Xavier F. Gouchet

09/28/2019, 4:14 PM
Indeed that does work 🙂
Thanks
p

pg

09/30/2019, 7:49 AM
@Xavier F. Gouchet can you share gist or something how you setup
detekt
in
buildSrc
? It's exactly what I tried to achieve yesterday without success 😓
x

Xavier F. Gouchet

09/30/2019, 8:15 AM
Sure thing, here's how I did it : in your
buildSrc/build.gradle.kts
:
dependencies {
    compile ("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.1")
 }
👍 1
And inside the
buildSrc/src/main/kotlin/com/example/DetektConfig.kt
package com.example

import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import org.gradle.api.Project

fun Project.detektConfig() {

     val ext: DetektExtension? = extensions.findByType(DetektExtension::class)
    ext?.apply {
        version = "1.0.1"

        input = files("$projectDir/src/main/kotlin")
        config = files("${project.rootDir}/script/config/detekt.yml")
        reports {
            xml {
                enabled = true
                destination = file("build/reports/detekt.xml")
            }
        }
    }
}
👍 1
Then you can just call
detektConfig()
from any
build.gradle.kts
script in any module
👍 2
t

tapchicoma

09/30/2019, 8:20 AM
as alternative you can create a plugin in
buildSrc
that configures detekt (and some other common stuff) and apply it to all modules
👍 2
p

pg

09/30/2019, 9:22 AM
@Xavier F. Gouchet ah that was the trick. Initially I tried to use approach in your first post, failed of course. I didn't think about creating project extension, which seems nice. @tapchicoma I thought about it, but something was telling me that creating wrapper plugin on 3rd party plugin isn't right 😅
t

tapchicoma

09/30/2019, 9:23 AM
it is not so bad as it sounds 🙂
but yeah, for simple logic just go with
Project
extension functions in the
buildSrc
x

Xavier F. Gouchet

09/30/2019, 9:25 AM
Yeah I found that just to add a configuration, a full Plugin might be overkill 🙂
p

pg

09/30/2019, 9:31 AM
I will experiment a bit with it. For now I created 2 plugins: one for
<http://android.app|android.app>
with
android-kotlin
and one for pure kotlin library, both with default configs and core deps. Effect is lovely
👍 1
h

henrik

09/30/2019, 11:24 AM
@Xavier F. Gouchet: Do you need an import in your build.gradle.kts files to gain access to
detektConfig()
?
x

Xavier F. Gouchet

09/30/2019, 11:30 AM
Yes you need to import the method
👍 1
h

henrik

09/30/2019, 11:37 AM
ah, after some trial and error I figured out that I didn't actually have to import it 🙂