Set jvm target validation to error only for builds with Gradle 8+
^KT54993 Fixed
This commit is contained in:
committed by
Space Team
parent
737fdf31f9
commit
4d20b43619
+67
-4
@@ -13,6 +13,7 @@ import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.testbase.*
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import java.io.File
|
||||
import kotlin.io.path.appendText
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.deleteExisting
|
||||
import kotlin.io.path.writeText
|
||||
@@ -32,10 +33,18 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
setJavaCompilationCompatibility(JavaVersion.VERSION_1_8)
|
||||
useToolchainToCompile(11)
|
||||
|
||||
gradleProperties.append(
|
||||
"""
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
buildAndFail("assemble") {
|
||||
assertOutputContains(
|
||||
"'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatibility " +
|
||||
"should be set to the same Java version.\n" +
|
||||
"By default will become an error since Gradle 8.0+! " +
|
||||
"Read more: https://kotl.in/gradle/jvm/target-validation\n" +
|
||||
"Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain"
|
||||
)
|
||||
}
|
||||
@@ -62,8 +71,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
build("assemble") {
|
||||
assertOutputContains(
|
||||
"'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatibility " +
|
||||
"should be set to the same Java version.\n" +
|
||||
"Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain"
|
||||
"should be set to the same Java version.\n"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -106,6 +114,12 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
) {
|
||||
useToolchainToCompile(11)
|
||||
|
||||
gradleProperties.append(
|
||||
"""
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build("build") {
|
||||
assertOutputDoesNotContain(
|
||||
"'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatibility " +
|
||||
@@ -183,6 +197,12 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
gradleProperties.append(
|
||||
"""
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build("build")
|
||||
}
|
||||
}
|
||||
@@ -197,6 +217,12 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
setJavaCompilationCompatibility(JavaVersion.VERSION_1_8)
|
||||
useToolchainToCompile(11)
|
||||
|
||||
gradleProperties.append(
|
||||
"""
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
kotlinSourcesDir().toFile().deleteRecursively()
|
||||
javaSourcesDir().resolve("demo/HelloWorld.java").deleteExisting()
|
||||
|
||||
@@ -239,8 +265,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
build("assemble") {
|
||||
assertOutputContains(
|
||||
"'compileJava' task (current target is 11) and 'compileKotlin' task (current target is 1.8) jvm target compatibility " +
|
||||
"should be set to the same Java version.\n" +
|
||||
"Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain"
|
||||
"should be set to the same Java version.\n"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -309,6 +334,13 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
} else {
|
||||
16
|
||||
}
|
||||
|
||||
gradleProperties.append(
|
||||
"""
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
@@ -324,6 +356,37 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("Default value becomes 'error' with Gradle 8+")
|
||||
@GradleTestVersions(maxVersion = TestVersions.Gradle.G_8_0)
|
||||
@GradleTest
|
||||
internal fun errorByDefaultWithGradle8(gradleVersion: GradleVersion) {
|
||||
project("simple".fullProjectName, gradleVersion) {
|
||||
//language=Groovy
|
||||
buildGradle.appendText(
|
||||
"""
|
||||
|
|
||||
|tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile.class).configureEach {
|
||||
| compilerOptions.jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11)
|
||||
|}
|
||||
""".trimMargin()
|
||||
)
|
||||
|
||||
if (gradleVersion.baseVersion >= GradleVersion.version("8.0")) {
|
||||
buildAndFail("assemble") {
|
||||
assertOutputContains(
|
||||
"'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatibility should be set to the same Java version."
|
||||
)
|
||||
}
|
||||
} else {
|
||||
build("assemble") {
|
||||
assertOutputContains(
|
||||
"'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11) jvm target compatibility should be set to the same Java version."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun TestProject.setJavaCompilationCompatibility(
|
||||
target: JavaVersion
|
||||
) {
|
||||
|
||||
+3
@@ -19,6 +19,9 @@ interface TestVersions {
|
||||
const val G_7_3 = "7.3.3"
|
||||
const val G_7_4 = "7.4.2"
|
||||
const val G_7_5 = "7.5"
|
||||
// https://gradle.org/nightly/
|
||||
// Retention policy is 3 months
|
||||
const val G_8_0 = "8.0-20221126231700+0000"
|
||||
const val MIN_SUPPORTED = minSupportedGradleVersion
|
||||
const val MIN_SUPPORTED_KPM = G_7_0
|
||||
const val MAX_SUPPORTED = G_7_3
|
||||
|
||||
+5
-1
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.gradle.plugin
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties
|
||||
import org.jetbrains.kotlin.cli.common.toBooleanLenient
|
||||
import org.jetbrains.kotlin.gradle.dsl.NativeCacheKind
|
||||
@@ -424,7 +425,10 @@ internal class PropertiesProvider private constructor(private val project: Proje
|
||||
}
|
||||
|
||||
val jvmTargetValidationMode: JvmTargetValidationMode
|
||||
get() = enumProperty("kotlin.jvm.target.validation.mode", JvmTargetValidationMode.ERROR)
|
||||
get() = enumProperty(
|
||||
"kotlin.jvm.target.validation.mode",
|
||||
if (GradleVersion.current().baseVersion >= GradleVersion.version("8.0")) JvmTargetValidationMode.ERROR else JvmTargetValidationMode.WARNING
|
||||
)
|
||||
|
||||
val kotlinDaemonJvmArgs: String?
|
||||
get() = this.property("kotlin.daemon.jvmargs")
|
||||
|
||||
+14
-5
@@ -23,6 +23,7 @@ import org.gradle.api.tasks.compile.AbstractCompile
|
||||
import org.gradle.api.tasks.compile.JavaCompile
|
||||
import org.gradle.api.tasks.util.PatternFilterable
|
||||
import org.gradle.api.tasks.util.PatternSet
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.gradle.work.ChangeType
|
||||
import org.gradle.work.Incremental
|
||||
import org.gradle.work.InputChanges
|
||||
@@ -644,7 +645,7 @@ abstract class KotlinCompile @Inject constructor(
|
||||
@get:Internal
|
||||
internal abstract val associatedJavaCompileTaskName: Property<String>
|
||||
|
||||
@get:Internal
|
||||
@get:Input
|
||||
internal abstract val jvmTargetValidationMode: Property<PropertiesProvider.JvmTargetValidationMode>
|
||||
|
||||
@get:Internal
|
||||
@@ -794,10 +795,18 @@ abstract class KotlinCompile @Inject constructor(
|
||||
val jvmTarget = args.jvmTarget ?: JvmTarget.DEFAULT.toString()
|
||||
if (normalizedJavaTarget != jvmTarget) {
|
||||
val javaTaskName = associatedJavaCompileTaskName.get()
|
||||
val errorMessage = "'$javaTaskName' task (current target is $targetCompatibility) and " +
|
||||
"'$name' task (current target is $jvmTarget) " +
|
||||
"jvm target compatibility should be set to the same Java version.\n" +
|
||||
"Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain"
|
||||
|
||||
val errorMessage = buildString {
|
||||
append("'$javaTaskName' task (current target is $targetCompatibility) and ")
|
||||
append("'$name' task (current target is $jvmTarget) ")
|
||||
appendLine("jvm target compatibility should be set to the same Java version.")
|
||||
if (GradleVersion.current().baseVersion < GradleVersion.version("8.0")) {
|
||||
append("By default will become an error since Gradle 8.0+! ")
|
||||
appendLine("Read more: https://kotl.in/gradle/jvm/target-validation")
|
||||
}
|
||||
appendLine("Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain")
|
||||
}
|
||||
|
||||
when (jvmTargetValidationMode.get()) {
|
||||
PropertiesProvider.JvmTargetValidationMode.ERROR -> throw GradleException(errorMessage)
|
||||
PropertiesProvider.JvmTargetValidationMode.WARNING -> logger.warn(errorMessage)
|
||||
|
||||
Reference in New Issue
Block a user