Hello devs, I'm migrating all my gradle scripts fr...
# gradle
x
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 :
Copy code
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
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
I think you need to add 3rd party plugin as a dependency to your
buildSrc
x
Indeed that does work πŸ™‚
Thanks
p
@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
Sure thing, here's how I did it : in your
buildSrc/build.gradle.kts
:
Copy code
dependencies {
    compile ("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.1")
 }
πŸ‘ 1
And inside the
buildSrc/src/main/kotlin/com/example/DetektConfig.kt
Copy code
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
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
@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
it is not so bad as it sounds πŸ™‚
but yeah, for simple logic just go with
Project
extension functions in the
buildSrc
x
Yeah I found that just to add a configuration, a full Plugin might be overkill πŸ™‚
p
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
@Xavier F. Gouchet: Do you need an import in your build.gradle.kts files to gain access to
detektConfig()
?
x
Yes you need to import the method
πŸ‘ 1
h
ah, after some trial and error I figured out that I didn't actually have to import it πŸ™‚