Hi. How can I write the following in `build.gradle...
# gradle
d
Hi. How can I write the following in
build.gradle.kts
?
Copy code
sourceSets {
    main {
        java { srcDir "./src" }
        kotlin { srcDir "./src" }
        resources { srcDir "./resources" }
    }
    test {
        kotlin { srcDir "./test" }
    }
}
Maybe this will help
d
Unfortunately, no. Because
java.sourceSets["main"].kotlin
doesn't exist (probably needs some extension function import).
i
Copy code
java {
  (sourceSets) {
    "main" {
      withConvention(KotlinSourceSet::class) {
        kotlin.srcDirs("wherever")
      }
    }
  }
}
d
yes, just noticed
thank you! 👍
K 1
(can't say that I'm impressed by the new syntax though)
i
Yep, I think there are better option exists.
d
In case somebody wants to know exact answer to the question:
Copy code
java.sourceSets {
    "main" {
        java.srcDirs("./src")
        kotlin().srcDirs("./src")
        resources.srcDirs("./resources")
    }
    "test" {
        kotlin().srcDirs("./test")
    }
}
fun Any.kotlin() = withConvention(KotlinSourceSet::class) { kotlin }
g
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
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)