Kotlin DSL question: creating a new/modifying an existing task


(Stephen Tiilikainen) #1

I’m in the middle of converting a build script for a Java webapp from Groovy to Kotlin DSL and I’m running into a couple of issues. I started out with the time-honored method of renaming build.gradle to build.gradle.kts and fixing things as I went. The dependencies block went pretty smoothly once I figured out how I could define staticWebContent using val (see below) but I’m still having a significant number of issues.

My understanding is that since you can mix-and-match Groovy and Kotlin scripts in the same multi-project build, I thought it best to convert one of the scripts that one of the simpler subprojects uses.

Here, I hope, are the relevant portions of the script that I’m having trouble configuring annotated with comments.

plugins {
    war
}

val warArchive by configurations.creating
val staticWebContent by configurations.creating

dependencies {
    // snipped for the most part. I include the below because IntelliJ correctly recognizes
    // "staticWebContent" as a valid configuration
    staticWebContent(
        group = "mycorp",
        name = "mycorp_web_content",
        version = "${project.properties["mycorpWebContentVersion"]}",
        ext = "zip"
    )
}

// First bit of problematic code. Read on...
task copyStaticWebContent(type: Copy) { // this: Copy
    // Unresolved reference: processResources
    mustRunAfter(processResources)
    // I know this is not correct... I have a note from earlier research to use Kotlin's "map" function instead.
    from(configurations.arinWebContent.collect({
        zipTree(it)
    }))
    into("$buildDir/static_web_content/")
}

// Second bit of code that is problematic. "dependsOn", "from", "into", and "basename" are all unresolvable
war {
    dependsOn(copyStaticWebContent)
    from("$buildDir/static_web_content/media") {
        into("media/")
    }
    from("$buildDir/static_web_content/resources") {
        into("resources/")
    }

    baseName("myapp")
}

// Lastly... the only thing that isn't colored red here are the double-quoted strings.
// Nothing else is resolvable.
processResources {
    from("src/main/webapp") {
        include("**/*.xml")
        include("**/*.xsl")
        include("**/*.jsp")
        include("**/*.xhtml")
    }
}

My lack of understanding centers on how to configure/modify things that are originally created by plugins. This, from my research, seems to vary based upon how the (Groovy) plugin was originally written. I’m finding that the conversion process involves becoming an expert in both Groovy and Kotlin. I have decent facility with Java 8 lambda expressions, but I’m still getting up to speed with Kotlin, and Groovy is another beast altogether. Any help in improving my understanding would be greatly appreciated. This is not an undertaking for the faint-of-heart (or mind, for that matter).