https://kotlinlang.org logo
Title
s

sergei.lebedev

05/12/2017, 9:28 PM
Hi folks, is there a way to access sourceSets in the API? I’m translating the following Groovy bit
task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}
s

suresh

05/12/2017, 10:34 PM
sergei.lebedev:
val sourceSets = java.sourceSets
val SourceSetContainer.main: SourceSet get() = this["main"]
val SourceSetContainer.test: SourceSet get() = this["test"]


val SourceSet.kotlin: SourceDirectorySet get() =
    (this as HasConvention).convention.getPlugin<KotlinSourceSet>().kotlin
👍 1
now you can access the sourceset like
sourceSets.main
s

sergei.lebedev

05/12/2017, 10:38 PM
Do you think it is possible to make this part of the official API?
Also: I can’t reference
java
, it refers to
java
the package.
m

mkobit

05/12/2017, 11:12 PM
i’ve had some wonkiness with the extension method generation before, you could also do
the<JavaPluginConvention>().sourceSets
s

sergei.lebedev

05/12/2017, 11:14 PM
Thanks! This does indeed work. Could you elaborate a bit on extension generation, is it supposed to be dynamic?
m

mkobit

05/12/2017, 11:20 PM
GSK generates extension methods for the actual extensions and convention objects applied by the plugins. so say you have you apply the
java
plugin which creates a convention object named
java
of type
JavaPluginConvention
. then a top level extension method
Project.java(JavaPluginConvention.() -> Unit): Unit
(something like that) is generated. the same is true for
extension
objects https://github.com/gradle/gradle-script-kotlin/releases/tag/v0.8.0 is more thorough and probably a better explanation than i can give
s

suresh

05/13/2017, 2:14 AM
yeah for extension method generation , i always rely on
./gradlew gskGenerateAccessors
. After that you can refer
java
in your script.
s

sergei.lebedev

05/14/2017, 8:54 PM
I wonder why IntelliJ didn’t do this on import, hmm
Thanks @mkobit and @suresh!