https://kotlinlang.org logo
Title
d

Dmitry Kandalov

12/13/2017, 11:00 PM
Hi. How can I write the following in
build.gradle.kts
?
sourceSets {
    main {
        java { srcDir "./src" }
        kotlin { srcDir "./src" }
        resources { srcDir "./resources" }
    }
    test {
        kotlin { srcDir "./test" }
    }
}
Maybe this will help
d

Dmitry Kandalov

12/13/2017, 11:06 PM
Unfortunately, no. Because
java.sourceSets["main"].kotlin
doesn't exist (probably needs some extension function import).
i

irus

12/13/2017, 11:07 PM
java {
  (sourceSets) {
    "main" {
      withConvention(KotlinSourceSet::class) {
        kotlin.srcDirs("wherever")
      }
    }
  }
}
d

Dmitry Kandalov

12/13/2017, 11:08 PM
yes, just noticed
thank you! 👍
:kotlin-flag: 1
(can't say that I'm impressed by the new syntax though)
i

irus

12/13/2017, 11:09 PM
Yep, I think there are better option exists.
d

Dmitry Kandalov

12/13/2017, 11:14 PM
In case somebody wants to know exact answer to the question:
java.sourceSets {
    "main" {
        java.srcDirs("./src")
        kotlin().srcDirs("./src")
        resources.srcDirs("./resources")
    }
    "test" {
        kotlin().srcDirs("./test")
    }
}
fun Any.kotlin() = withConvention(KotlinSourceSet::class) { kotlin }
g

gildor

12/14/2017, 12:24 AM
Yes, because convention is legacy way to extend Gradle, there is a plan to deprecate them, but many plugins still use them, and not sure that there is an easy replacement,l for some cases. Problem that conventions are very dynamic, so hard to use them in Kotlin but very flexible, you can extend everything. Maybe this sourceset configuration should be moved to dsl of Kotlin plugin
BTW you can convert fun `Any.kotlin() `to
val Any.kotlin
and replace Any with actual target (not sure about class tho)
👍 2
d

Dmitry Kandalov

12/14/2017, 8:47 AM
yes, this works
val Any.kotlin: SourceDirectorySet get() = withConvention(KotlinSourceSet::class) { kotlin }
even though there is also
fun Project.kotlin(configure: KotlinProjectExtension.() -> Unit): Unit = ...
in
org.gradle.kotlin.dsl
(might keep it as a function for now so that it's more noticeable... on the other hand there are four things within the script called
kotlin
anyway,
val
won't make anything more confusing)