How can we split a huge `build.gradle.kts` script ...
# gradle
l
How can we split a huge
build.gradle.kts
script into multiple short scripts that configure plugins and tasks? Do
buildSrc
can help while https://github.com/gradle/kotlin-dsl/issues/424 and https://github.com/gradle/kotlin-dsl/issues/427 don't get fixed?
s
you mean like with apply?
c
Don't know your specifics, but I've found it very easy to implement Gradle Plugin interface in buildSrc and apply it via
plugins {}
. This approach really helped to properly structure the builds I've got on my hands. My build.gradle.kts files are now much leaner and utility functionality which earlier littered them is now much better looking as custom plugins in buildSrc.
o
buildSrc doesn't even need to be applied via
plugins {}
c
I believe it does, when you're implementing a plugin that is. You are right that one wouldn't need that for "normal" code though, like some classes or functions to call from the script.
l
Thank you all for suggestions. @snowe That seems nice. It seems to be the around for those issues. @Czar It's just a huge script with
plugins {}
block and tasks configuration. Here is the file https://github.com/lucasvalenteds/responder/blob/main/responder-api/build.gradle.kts.
Just in case, here is a POC for the issues linked, which I'm looking for a workaround. https://github.com/lucasvalenteds/gradle-kotlin-dsl-scripts
o
my workaround would be to just throw those in
buildSrc
, include the
kotlin-dsl
plugin in it, and wrap them in a plugin
apply
. you can apply the plugins using
Project.apply
and add them to the
buildSrc
classpath for versioning, which imo is a little cleaner (have your versions in one spot, usages in another)
perhaps not even make them a
plugin
, it's a little overkill
just have a
fun Project.shadowJarConfiguration() { ... }
and call that
l
Thank you @octylFractal. Do you mean something like this:
buildSrc/build.gradle.kts
Copy code
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    `kotlin-dsl`
    id("org.jetbrains.kotlin.jvm") version "1.3.11"
    id("com.github.johnrengelman.shadow") version "2.0.4"
}

repositories {
    jcenter()
}

fun Project.kotlinConfiguration() {
    tasks.withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }
}

fun Project.shadowJarConfiguration() {
    tasks.withType<ShadowJar> {
        baseName = rootProject.name
        version = rootProject.version.toString()
        classifier = ""
        destinationDir = rootProject.projectDir
    }
}
build.gradle.kts
Copy code
// Leave it empty
o
no, those `fun`s go in
src/main/kotlin/<pkg>
you should also
apply
in them
and you will need to write
build.gradle.kts
as:
Copy code
// <relevant imports, IntelliJ can add them for you>
kotlinConfiguration()
shadowJarConfiguration()
l
Thank you. I'll try that. 👍