Don't validate JVM target on empty Java sources.

When toolchain is not configured, Kotlin sets by default JVM target to
"1.8". Compiling Kotlin project on JDK 9+ produces JVM target mismatch
warning even if Java sources are empty.

Skip the jvm validation check if associated Java task sources are empty.

^KT-48745 Fixed
This commit is contained in:
Yahor Berdnikau
2021-09-20 17:14:47 +02:00
committed by Space
parent 708931b404
commit 8b594e295c
2 changed files with 102 additions and 17 deletions
@@ -532,6 +532,11 @@ abstract class KotlinCompile @Inject constructor(
task.associatedJavaCompileTaskTargetCompatibility.set(
compileJavaTaskProvider.map { it.targetCompatibility }
)
task.associatedJavaCompileTaskSources.from(
compileJavaTaskProvider.map { javaTask ->
javaTask.source
}
)
task.associatedJavaCompileTaskName.set(
compileJavaTaskProvider.map { it.name }
)
@@ -642,6 +647,9 @@ abstract class KotlinCompile @Inject constructor(
@get:Internal
internal abstract val associatedJavaCompileTaskTargetCompatibility: Property<String>
@get:Internal
internal abstract val associatedJavaCompileTaskSources: ConfigurableFileCollection
@get:Internal
internal abstract val associatedJavaCompileTaskName: Property<String>
@@ -733,24 +741,26 @@ abstract class KotlinCompile @Inject constructor(
}
private fun validateKotlinAndJavaHasSameTargetCompatibility(args: K2JVMCompilerArguments) {
associatedJavaCompileTaskTargetCompatibility.orNull?.let { targetCompatibility ->
val normalizedJavaTarget = when (targetCompatibility) {
"6" -> "1.6"
"7" -> "1.7"
"8" -> "1.8"
"1.9" -> "9"
else -> targetCompatibility
}
if (!associatedJavaCompileTaskSources.isEmpty) {
associatedJavaCompileTaskTargetCompatibility.orNull?.let { targetCompatibility ->
val normalizedJavaTarget = when (targetCompatibility) {
"6" -> "1.6"
"7" -> "1.7"
"8" -> "1.8"
"1.9" -> "9"
else -> targetCompatibility
}
if (normalizedJavaTarget != args.jvmTarget) {
val javaTaskName = associatedJavaCompileTaskName.get()
val errorMessage = "'$javaTaskName' task (current target is $targetCompatibility) and " +
"'$name' task (current target is ${args.jvmTarget}) " +
"jvm target compatibility should be set to the same Java version."
when (jvmTargetValidationMode.get()) {
PropertiesProvider.JvmTargetValidationMode.ERROR -> throw GradleException(errorMessage)
PropertiesProvider.JvmTargetValidationMode.WARNING -> logger.warn(errorMessage)
else -> Unit
if (normalizedJavaTarget != args.jvmTarget) {
val javaTaskName = associatedJavaCompileTaskName.get()
val errorMessage = "'$javaTaskName' task (current target is $targetCompatibility) and " +
"'$name' task (current target is ${args.jvmTarget}) " +
"jvm target compatibility should be set to the same Java version."
when (jvmTargetValidationMode.get()) {
PropertiesProvider.JvmTargetValidationMode.ERROR -> throw GradleException(errorMessage)
PropertiesProvider.JvmTargetValidationMode.WARNING -> logger.warn(errorMessage)
else -> Unit
}
}
}
}