Search code examples
javagradlegroovykotlingradle-kotlin-dsl

Gradle Kotlin DSL: Extract contents from dependency


How do I convert the following snippet (based on the one by Peter Niederwieser) to Kotlin?

configurations {
    assets
}
dependencies {
    assets 'somegroup:someArtifact:someVersion'
}
task extractApi(type: Sync) {
    dependsOn configurations.assets

    from { // use of closure defers evaluation until execution time
        configurations.assets.collect { zipTree(it) }
    }
    into "$buildDir/assets/"
}

Solution

  • I haven't experience with the Kotlin DSL, but apparently the extractApi task could be re-written as

    val assets by configurations.creating
    
    dependencies {
        assets("somegroup", "someArtifact", "someVersion")
    }
    
    tasks {
        val extractApi by creating(Sync::class) {
            dependsOn(assets)
    
            from(assets.map {
                zipTree(it)
            })
    
            into("$buildDir/api/")
        }
    }