0

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/"
}
Michel Krämer
  • 14,197
  • 5
  • 32
  • 32

1 Answers1

1

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/")
    }
}
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • I already tried that. It gives me the following error: `Line 145: from() { ^ Passing value as a vararg is only allowed inside a parenthesized argument list` – Michel Krämer Feb 22 '19 at 19:51
  • @MichelKrämer And without "()"? – LppEdd Feb 22 '19 at 19:52
  • I suppose you mean /with/ parentheses because the error message clearly indicates that parentheses are needed. If I leave them off, I get the same error message. If I add them around the closure I get: `Cannot convert the provided notation to a File or URI: () -> kotlin.collections.List` – Michel Krämer Feb 22 '19 at 19:54
  • @MichelKrämer okay. Well I'll just create a new project using Kotlin DSL. Let's see – LppEdd Feb 22 '19 at 19:55
  • Thanks, but this code doesn't do anything. What does `path needed!` mean? – Michel Krämer Feb 22 '19 at 20:27
  • @MichelKrämer well apparently an argument to the "from" method must be always provided. You cannot leave it out. I had a look at the sources and it seems so – LppEdd Feb 22 '19 at 20:28
  • Yes, but did you also test the build script? AFAICT it does not copy anything. – Michel Krämer Feb 22 '19 at 20:29
  • @MichelKrämer done. Tested and working, more or less. May need some adjustements. But seems the right solution to me – LppEdd Feb 22 '19 at 20:46
  • Sorry, I didn't remember to include the "dependsOn" – LppEdd Feb 22 '19 at 20:52
  • Thanks for the answer. It works indeed. I was wondering about the `isCanBeResolved`. I replaced it with `dependsOn` and it still works. I think `dependsOn` is the cleaner solution. If you update your answer, I'd more than happy to accept it. – Michel Krämer Feb 23 '19 at 08:57
  • @MichelKrämer Thank you. Before editing the answer I'd like to understand better how you got it working. I had to add it, to avoid the error "Resolving configuration 'apiElements' directly is not allowed" – LppEdd Feb 23 '19 at 09:02
  • Ah, I see. My configuration is called `assets`. I had to create it with `val assets by configurations.creating`. After that I did not need the `isCanBeResolved` anymore. By the way, `isCanBeResolved` is deprecated and will be removed in Gradle 6. – Michel Krämer Feb 23 '19 at 09:09
  • I'll update the question so that it also uses `assets` instead of `api` – Michel Krämer Feb 23 '19 at 09:11
  • @MichelKrämer ahhh so it was a custom configuration, not the included "api" one (which is a recent addition to Gradle) – LppEdd Feb 23 '19 at 09:12
  • @MichelKrämer done! Btw, thank you, I had to study a bit of the Kotlin DSL and it was fun – LppEdd Feb 23 '19 at 09:16