11/*
22 * Shared JVM conventions for all subprojects.
3- *
4- * Kotlin DSL alternative to gradle/jvm-conventions.gradle.
5- * Not wired into the build by default.
63 */
74
85import org.gradle.api.JavaVersion
6+ import org.gradle.api.plugins.JavaPluginExtension
97import org.gradle.api.tasks.compile.JavaCompile
108import org.gradle.jvm.toolchain.JavaLanguageVersion
119import org.gradle.api.tasks.testing.Test
12- import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
1310
1411val javaToolchainVersion = rootProject.extra[" javaToolchainVersion" ] as Int
1512val javaTargetVersion = rootProject.extra[" javaTargetVersion" ] as JavaVersion
13+ val kotlinCompileTaskNames = setOf (" compileKotlin" , " compileTestKotlin" )
1614
1715subprojects {
1816 apply (plugin = " java-library" )
1917 apply (plugin = " kotlin" )
2018 apply (plugin = " com.github.ben-manes.versions" )
2119 apply (plugin = " se.patrikerdes.use-latest-versions" )
2220
23- java {
21+ extensions.configure( JavaPluginExtension :: class . java) {
2422 toolchain {
2523 languageVersion.set(JavaLanguageVersion .of(javaToolchainVersion))
2624 }
@@ -43,19 +41,30 @@ subprojects {
4341 }
4442 }
4543
46- tasks.withType(KotlinJvmCompile ::class .java).configureEach {
47- compilerOptions {
48- jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget .fromTarget(javaTargetVersion.toString()))
44+ tasks
45+ .matching { it.name in kotlinCompileTaskNames }
46+ .configureEach {
47+ val kotlinOptions = if (hasProperty(" kotlinOptions" )) property(" kotlinOptions" ) else null
48+ if (kotlinOptions != null ) {
49+ kotlinOptions.javaClass.getMethod(" setJvmTarget" , String ::class .java)
50+ .invoke(kotlinOptions, javaTargetVersion.toString())
4951
50- val current = freeCompilerArgs.get().toMutableList()
51- current.removeAll { it == " -Xjvm-default=all" }
52- if (! current.contains(" -jvm-default=no-compatibility" )) {
53- current.add(" -jvm-default=no-compatibility" )
54- }
55- if (! current.contains(" -Xconsistent-data-class-copy-visibility" )) {
56- current.add(" -Xconsistent-data-class-copy-visibility" )
52+ @Suppress(" UNCHECKED_CAST" )
53+ val args = (kotlinOptions.javaClass.getMethod(" getFreeCompilerArgs" ).invoke(kotlinOptions) as ? List <String >).orEmpty()
54+ val updated = args
55+ .filterNot { it == " -Xjvm-default=all" }
56+ .toMutableList()
57+ .also {
58+ if (! it.contains(" -jvm-default=no-compatibility" )) {
59+ it.add(" -jvm-default=no-compatibility" )
60+ }
61+ if (! it.contains(" -Xconsistent-data-class-copy-visibility" )) {
62+ it.add(" -Xconsistent-data-class-copy-visibility" )
63+ }
64+ }
65+
66+ kotlinOptions.javaClass.getMethod(" setFreeCompilerArgs" , List ::class .java)
67+ .invoke(kotlinOptions, updated)
5768 }
58- freeCompilerArgs.set(current)
5969 }
60- }
6170}
0 commit comments