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