Raise 'kotlin.jvm.target.validation.mode' default value to 'error'.
Now target check intentionally runs always - even when Java sources are empty. Java part configures publication 'org.gradle.jvm.version' attribute which affect published artifact consumers and should be the same as Kotlin jvmTarget value. ^KT-54993 Fixed
This commit is contained in:
committed by
Space Team
parent
877e11419e
commit
e07e92c945
@@ -110,6 +110,11 @@ task testJdk6Tests(type: Test) { thisTask ->
|
||||
})
|
||||
}
|
||||
|
||||
tasks.named("compileModuleTestJava", JavaCompile) {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_9
|
||||
targetCompatibility = JavaVersion.VERSION_1_9
|
||||
}
|
||||
|
||||
compileModuleTestKotlin {
|
||||
kotlinJavaToolchain.toolchain.use(JvmToolchain.getToolchainLauncherFor(project, JdkMajorVersion.JDK_9_0))
|
||||
}
|
||||
|
||||
+31
-37
@@ -21,9 +21,9 @@ import kotlin.io.path.writeText
|
||||
@JvmGradlePluginTests
|
||||
class JvmTargetValidationTest : KGPBaseTest() {
|
||||
|
||||
@DisplayName("Should produce warning if java and kotlin jvm targets are different")
|
||||
@DisplayName("Should produce error if java and kotlin jvm targets are different")
|
||||
@GradleTest
|
||||
internal fun shouldWarnIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
||||
internal fun shouldFailIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
||||
project(
|
||||
projectName = "kotlinJavaProject".fullProjectName,
|
||||
gradleVersion = gradleVersion,
|
||||
@@ -32,18 +32,19 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
setJavaCompilationCompatibility(JavaVersion.VERSION_1_8)
|
||||
useToolchainToCompile(11)
|
||||
|
||||
build("assemble") {
|
||||
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."
|
||||
"should be set to the same Java version.\n" +
|
||||
"Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("Should fail the build if verification mode is 'error' and kotlin and java targets are different")
|
||||
@DisplayName("Should warn in the build log if verification mode is 'warning' and kotlin and java targets are different")
|
||||
@GradleTest
|
||||
internal fun shouldFailBuildIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
||||
internal fun shouldWarnBuildIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
||||
project(
|
||||
projectName = "kotlinJavaProject".fullProjectName,
|
||||
gradleVersion = gradleVersion
|
||||
@@ -54,14 +55,15 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
buildAndFail("assemble") {
|
||||
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."
|
||||
"should be set to the same Java version.\n" +
|
||||
"Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -124,7 +126,14 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
) {
|
||||
useToolchainToCompile(11)
|
||||
|
||||
JavaVersion.VERSION_1_8
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
//language=Groovy
|
||||
buildGradle.append(
|
||||
"""
|
||||
@@ -173,13 +182,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build("build")
|
||||
}
|
||||
@@ -194,13 +196,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
) {
|
||||
setJavaCompilationCompatibility(JavaVersion.VERSION_1_8)
|
||||
useToolchainToCompile(11)
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
kotlinSourcesDir().toFile().deleteRecursively()
|
||||
javaSourcesDir().resolve("demo/HelloWorld.java").deleteExisting()
|
||||
|
||||
@@ -224,9 +220,9 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@DisplayName("Should skip JVM target validation if no java sources are available")
|
||||
@DisplayName("Should still do JVM target validation if no java sources are available")
|
||||
@GradleTest
|
||||
internal fun shouldSkipJvmTargetValidationNoJavaSources(gradleVersion: GradleVersion) {
|
||||
internal fun shouldDoJvmTargetValidationOnNoJavaSources(gradleVersion: GradleVersion) {
|
||||
project(
|
||||
projectName = "simple".fullProjectName,
|
||||
gradleVersion = gradleVersion,
|
||||
@@ -236,11 +232,17 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build("assemble")
|
||||
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"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,7 +266,7 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
)
|
||||
|
||||
build("assemble") {
|
||||
assertOutputDoesNotContain(
|
||||
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."
|
||||
)
|
||||
}
|
||||
@@ -302,14 +304,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
||||
gradleVersion = gradleVersion,
|
||||
buildJdk = getJdk11().javaHome
|
||||
) {
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = error
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
val toolchainJavaVersion = if (gradleVersion < GradleVersion.version("6.9")) {
|
||||
15
|
||||
} else {
|
||||
|
||||
+14
-7
@@ -33,6 +33,7 @@ import org.junit.jupiter.api.condition.OS
|
||||
import java.nio.file.Files
|
||||
import java.util.zip.ZipFile
|
||||
import java.util.zip.ZipOutputStream
|
||||
import kotlin.io.path.appendText
|
||||
import kotlin.io.path.deleteExisting
|
||||
import kotlin.io.path.outputStream
|
||||
import kotlin.test.assertEquals
|
||||
@@ -139,6 +140,17 @@ open class Kapt3IT : Kapt3BaseIT() {
|
||||
gradleVersion,
|
||||
buildJdk = jdk.location
|
||||
) {
|
||||
//language=Groovy
|
||||
buildGradle.appendText(
|
||||
"""
|
||||
|
|
||||
|java {
|
||||
| sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
| targetCompatibility = JavaVersion.VERSION_1_8
|
||||
|}
|
||||
""".trimMargin()
|
||||
)
|
||||
|
||||
build("assemble") {
|
||||
assertTasksExecuted(":kaptGenerateStubsKotlin", ":kaptKotlin")
|
||||
// Check added because of https://youtrack.jetbrains.com/issue/KT-33056.
|
||||
@@ -929,16 +941,11 @@ open class Kapt3IT : Kapt3BaseIT() {
|
||||
}
|
||||
|
||||
@DisplayName("Works with JPMS on JDK 9+")
|
||||
@JdkVersions(versions = [JavaVersion.VERSION_11])
|
||||
@GradleWithJdkTest
|
||||
fun testJpmsModule(
|
||||
gradleVersion: GradleVersion,
|
||||
jdk: JdkVersions.ProvidedJdk
|
||||
) {
|
||||
@GradleTest
|
||||
fun testJpmsModule(gradleVersion: GradleVersion, ) {
|
||||
project(
|
||||
"jpms-module".withPrefix,
|
||||
gradleVersion,
|
||||
buildJdk = jdk.location
|
||||
) {
|
||||
build("assemble") {
|
||||
assertTasksExecuted(":kaptKotlin", ":kaptGenerateStubsKotlin", ":compileKotlin", ":compileJava")
|
||||
|
||||
+1
-1
@@ -165,7 +165,7 @@ class KaptIncrementalWithIsolatingApt : KaptIncrementalIT() {
|
||||
}
|
||||
|
||||
@DisplayName("KT-33617: sources in compile classpath jars")
|
||||
@JdkVersions(versions = [JavaVersion.VERSION_1_9])
|
||||
@JdkVersions(versions = [JavaVersion.VERSION_11])
|
||||
@GradleWithJdkTest
|
||||
fun testSourcesInCompileClasspathJars(gradleVersion: GradleVersion, jdk: JdkVersions.ProvidedJdk) {
|
||||
kaptProject(gradleVersion, buildJdk = jdk.location) {
|
||||
|
||||
+24
@@ -357,6 +357,14 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
JavaVersion.VERSION_11
|
||||
)
|
||||
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build("build") {
|
||||
assertOutputContains("-jvm-target 11")
|
||||
assertOutputDoesNotContain("-jvm-target 1.8")
|
||||
@@ -414,6 +422,14 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
setJvmTarget("1.8")
|
||||
useToolchainToCompile(11)
|
||||
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build("build") {
|
||||
assertOutputContains("-jvm-target 1.8")
|
||||
assertOutputDoesNotContain("-jvm-target 11")
|
||||
@@ -553,6 +569,14 @@ class KotlinJavaToolchainTest : KGPBaseTest() {
|
||||
gradleVersion = gradleVersion,
|
||||
buildJdk = getJdk11().javaHome
|
||||
) {
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
//language=Groovy
|
||||
buildGradle.append(
|
||||
"""
|
||||
|
||||
+8
@@ -65,6 +65,14 @@ class UpToDateIT : KGPBaseTest() {
|
||||
mutations: Set<ProjectMutation>
|
||||
) {
|
||||
project("kotlinProject", gradleVersion) {
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
mutations.forEach { mutation ->
|
||||
mutation.initProject(this)
|
||||
build("classes")
|
||||
|
||||
+8
@@ -131,6 +131,14 @@ class Kapt3AndroidIT : Kapt3BaseIT() {
|
||||
buildOptions = defaultBuildOptions.copy(androidVersion = agpVersion),
|
||||
buildJdk = jdkVersion.location
|
||||
) {
|
||||
//language=properties
|
||||
gradleProperties.append(
|
||||
"""
|
||||
# suppress inspection "UnusedProperty"
|
||||
kotlin.jvm.target.validation.mode = warning
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
buildGradle.appendText(
|
||||
"""
|
||||
apply plugin: 'kotlin-kapt'
|
||||
|
||||
+6
-1
@@ -2,4 +2,9 @@ apply plugin: 'kotlin'
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
+5
@@ -1,3 +1,8 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.jvm"
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
+6
-1
@@ -2,4 +2,9 @@ apply plugin: 'kotlin'
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
+4
@@ -23,6 +23,10 @@ dependencies {
|
||||
|
||||
compileKotlin.kotlinOptions.allWarningsAsErrors = true
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(11)
|
||||
}
|
||||
|
||||
compileJava {
|
||||
doFirst {
|
||||
options.compilerArgs += [
|
||||
|
||||
+1
-1
@@ -16,4 +16,4 @@ dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
}
|
||||
|
||||
compileKotlin.kotlinOptions.allWarningsAsErrors = true
|
||||
compileKotlin.kotlinOptions.allWarningsAsErrors = true
|
||||
|
||||
+5
@@ -12,6 +12,11 @@ repositories {
|
||||
}
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
|
||||
kapt "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
|
||||
|
||||
+5
@@ -12,6 +12,11 @@ repositories {
|
||||
}
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
|
||||
|
||||
+4
@@ -25,6 +25,10 @@ android {
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
+1
-1
@@ -417,7 +417,7 @@ internal class PropertiesProvider private constructor(private val project: Proje
|
||||
}
|
||||
|
||||
val jvmTargetValidationMode: JvmTargetValidationMode
|
||||
get() = enumProperty("kotlin.jvm.target.validation.mode", JvmTargetValidationMode.WARNING)
|
||||
get() = enumProperty("kotlin.jvm.target.validation.mode", JvmTargetValidationMode.ERROR)
|
||||
|
||||
val kotlinDaemonJvmArgs: String?
|
||||
get() = this.property("kotlin.daemon.jvmargs")
|
||||
|
||||
+20
-27
@@ -641,9 +641,6 @@ 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,7 +730,7 @@ abstract class KotlinCompile @Inject constructor(
|
||||
inputChanges: InputChanges,
|
||||
taskOutputsBackup: TaskOutputsBackup?
|
||||
) {
|
||||
validateKotlinAndJavaHasSameTargetCompatibility(args, kotlinSources)
|
||||
validateKotlinAndJavaHasSameTargetCompatibility(args)
|
||||
|
||||
val scriptSources = scriptSources.asFileTree.files
|
||||
val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors,)
|
||||
@@ -784,31 +781,27 @@ abstract class KotlinCompile @Inject constructor(
|
||||
|
||||
private fun validateKotlinAndJavaHasSameTargetCompatibility(
|
||||
args: K2JVMCompilerArguments,
|
||||
kotlinSources: Set<File>
|
||||
) {
|
||||
val mixedSourcesArePresent = !associatedJavaCompileTaskSources.isEmpty &&
|
||||
kotlinSources.isNotEmpty()
|
||||
if (mixedSourcesArePresent) {
|
||||
associatedJavaCompileTaskTargetCompatibility.orNull?.let { targetCompatibility ->
|
||||
val normalizedJavaTarget = when (targetCompatibility) {
|
||||
"6" -> "1.6"
|
||||
"7" -> "1.7"
|
||||
"8" -> "1.8"
|
||||
"1.9" -> "9"
|
||||
else -> targetCompatibility
|
||||
}
|
||||
associatedJavaCompileTaskTargetCompatibility.orNull?.let { targetCompatibility ->
|
||||
val normalizedJavaTarget = when (targetCompatibility) {
|
||||
"6" -> "1.6"
|
||||
"7" -> "1.7"
|
||||
"8" -> "1.8"
|
||||
"1.9" -> "9"
|
||||
else -> targetCompatibility
|
||||
}
|
||||
|
||||
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."
|
||||
when (jvmTargetValidationMode.get()) {
|
||||
PropertiesProvider.JvmTargetValidationMode.ERROR -> throw GradleException(errorMessage)
|
||||
PropertiesProvider.JvmTargetValidationMode.WARNING -> logger.warn(errorMessage)
|
||||
else -> Unit
|
||||
}
|
||||
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"
|
||||
when (jvmTargetValidationMode.get()) {
|
||||
PropertiesProvider.JvmTargetValidationMode.ERROR -> throw GradleException(errorMessage)
|
||||
PropertiesProvider.JvmTargetValidationMode.WARNING -> logger.warn(errorMessage)
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -72,7 +72,6 @@ internal open class BaseKotlinCompileConfig<TASK : KotlinCompile> : AbstractKotl
|
||||
taskProvider.configure { task ->
|
||||
javaTaskProvider?.let {
|
||||
task.associatedJavaCompileTaskTargetCompatibility.value(javaTaskProvider.map { it.targetCompatibility })
|
||||
task.associatedJavaCompileTaskSources.from(javaTaskProvider.map { it.source })
|
||||
task.associatedJavaCompileTaskName.value(javaTaskProvider.name)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user