Properly set jvmTarget from toolchain for KaptGenerateStubsTask
^KT-51415 Fixed
This commit is contained in:
+37
-9
@@ -13,7 +13,6 @@ import org.gradle.util.GradleVersion
|
|||||||
import org.jetbrains.kotlin.gradle.testbase.*
|
import org.jetbrains.kotlin.gradle.testbase.*
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.io.path.ExperimentalPathApi
|
|
||||||
import kotlin.io.path.createDirectories
|
import kotlin.io.path.createDirectories
|
||||||
import kotlin.io.path.deleteExisting
|
import kotlin.io.path.deleteExisting
|
||||||
import kotlin.io.path.writeText
|
import kotlin.io.path.writeText
|
||||||
@@ -23,7 +22,6 @@ import kotlin.io.path.writeText
|
|||||||
class JvmTargetValidationTest : KGPBaseTest() {
|
class JvmTargetValidationTest : KGPBaseTest() {
|
||||||
|
|
||||||
@DisplayName("Should produce warning if java and kotlin jvm targets are different")
|
@DisplayName("Should produce warning if java and kotlin jvm targets are different")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun shouldWarnIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
internal fun shouldWarnIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
||||||
project(
|
project(
|
||||||
@@ -44,7 +42,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DisplayName("Should fail the build if verification mode is 'error' and kotlin and java targets are different")
|
@DisplayName("Should fail the build if verification mode is 'error' and kotlin and java targets are different")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun shouldFailBuildIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
internal fun shouldFailBuildIfJavaAndKotlinJvmTargetsAreDifferent(gradleVersion: GradleVersion) {
|
||||||
project(
|
project(
|
||||||
@@ -71,7 +68,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DisplayName("Should ignore if verification mode is 'ignore' and kotlin and java targets are different")
|
@DisplayName("Should ignore if verification mode is 'ignore' and kotlin and java targets are different")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun shouldNotPrintAnythingIfJavaAndKotlinJvmTargetsAreDifferent(
|
internal fun shouldNotPrintAnythingIfJavaAndKotlinJvmTargetsAreDifferent(
|
||||||
gradleVersion: GradleVersion
|
gradleVersion: GradleVersion
|
||||||
@@ -100,7 +96,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DisplayName("Should not produce warning when java and kotlin jvm targets are the same")
|
@DisplayName("Should not produce warning when java and kotlin jvm targets are the same")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun shouldNotWarnOnJavaAndKotlinSameJvmTargets(gradleVersion: GradleVersion) {
|
internal fun shouldNotWarnOnJavaAndKotlinSameJvmTargets(gradleVersion: GradleVersion) {
|
||||||
project(
|
project(
|
||||||
@@ -119,7 +114,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DisplayName("Should produce Java-Kotlin jvm target incompatibility warning only for related tasks")
|
@DisplayName("Should produce Java-Kotlin jvm target incompatibility warning only for related tasks")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun shouldProduceJavaKotlinJvmTargetDifferenceWarningOnlyForRelatedTasks(
|
internal fun shouldProduceJavaKotlinJvmTargetDifferenceWarningOnlyForRelatedTasks(
|
||||||
gradleVersion: GradleVersion
|
gradleVersion: GradleVersion
|
||||||
@@ -161,7 +155,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DisplayName("Should correctly validate JVM targets in mixed Kotlin/Java projects that are using <JDK1.8")
|
@DisplayName("Should correctly validate JVM targets in mixed Kotlin/Java projects that are using <JDK1.8")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun oldJdkMixedJavaKotlinTargetVerification(gradleVersion: GradleVersion) {
|
internal fun oldJdkMixedJavaKotlinTargetVerification(gradleVersion: GradleVersion) {
|
||||||
project(
|
project(
|
||||||
@@ -193,7 +186,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DisplayName("Should skip JVM target validation if no Kotlin sources are available")
|
@DisplayName("Should skip JVM target validation if no Kotlin sources are available")
|
||||||
@GradleTestVersions(minVersion = TestVersions.Gradle.G_6_7)
|
|
||||||
@GradleTest
|
@GradleTest
|
||||||
internal fun shouldSkipJvmTargetValidationNoKotlinSources(gradleVersion: GradleVersion) {
|
internal fun shouldSkipJvmTargetValidationNoKotlinSources(gradleVersion: GradleVersion) {
|
||||||
project(
|
project(
|
||||||
@@ -254,7 +246,6 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
|
|
||||||
@DisplayName("Should do JVM target validation if java sources are added and configuration cache is reused")
|
@DisplayName("Should do JVM target validation if java sources are added and configuration cache is reused")
|
||||||
@GradleTest
|
@GradleTest
|
||||||
@ExperimentalPathApi
|
|
||||||
internal fun shouldDoJvmTargetValidationOnNewJavaSourcesAndConfigurationCacheReuse(gradleVersion: GradleVersion) {
|
internal fun shouldDoJvmTargetValidationOnNewJavaSourcesAndConfigurationCacheReuse(gradleVersion: GradleVersion) {
|
||||||
project(
|
project(
|
||||||
projectName = "simple".fullProjectName,
|
projectName = "simple".fullProjectName,
|
||||||
@@ -302,6 +293,43 @@ class JvmTargetValidationTest : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DisplayName("Validation should work correctly for KaptGenerateStubs task")
|
||||||
|
@GradleTestVersions
|
||||||
|
@GradleTest
|
||||||
|
internal fun kaptGenerateStubsValidateCorrect(gradleVersion: GradleVersion) {
|
||||||
|
project(
|
||||||
|
projectName = "kapt2/simple",
|
||||||
|
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 {
|
||||||
|
16
|
||||||
|
}
|
||||||
|
buildGradle.append(
|
||||||
|
"""
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvmToolchain {
|
||||||
|
languageVersion.set(JavaLanguageVersion.of($toolchainJavaVersion))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
|
||||||
|
build("assemble", forceOutput = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun TestProject.setJavaCompilationCompatibility(
|
private fun TestProject.setJavaCompilationCompatibility(
|
||||||
target: JavaVersion
|
target: JavaVersion
|
||||||
) {
|
) {
|
||||||
|
|||||||
+15
@@ -26,6 +26,7 @@ import org.gradle.work.NormalizeLineEndings
|
|||||||
import org.gradle.workers.WorkerExecutor
|
import org.gradle.workers.WorkerExecutor
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl
|
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl
|
||||||
|
import org.jetbrains.kotlin.gradle.report.BuildReportMode
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs
|
import org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
import org.jetbrains.kotlin.gradle.tasks.toSingleCompilerPluginOptions
|
import org.jetbrains.kotlin.gradle.tasks.toSingleCompilerPluginOptions
|
||||||
@@ -123,6 +124,20 @@ abstract class KaptGenerateStubsTask @Inject constructor(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Also use KotlinOptions configuration that was directly set to this task
|
||||||
|
// as 'compileKotlinArgumentsContributor' has KotlinOptions from linked KotlinCompile task
|
||||||
|
listOfNotNull(kotlinOptions, parentKotlinOptions.orNull)
|
||||||
|
.map { it as KotlinJvmOptionsImpl }
|
||||||
|
.forEach {
|
||||||
|
it.updateArguments(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copied from KotlinCompile
|
||||||
|
defaultKotlinJavaToolchain.get().updateJvmTarget(this, args)
|
||||||
|
if (reportingSettings().buildReportMode == BuildReportMode.VERBOSE) {
|
||||||
|
args.reportPerf = true
|
||||||
|
}
|
||||||
|
|
||||||
val pluginOptionsWithKapt = pluginOptions.toSingleCompilerPluginOptions().withWrappedKaptOptions(withApClasspath = kaptClasspath)
|
val pluginOptionsWithKapt = pluginOptions.toSingleCompilerPluginOptions().withWrappedKaptOptions(withApClasspath = kaptClasspath)
|
||||||
args.pluginOptions = (pluginOptionsWithKapt.arguments).toTypedArray()
|
args.pluginOptions = (pluginOptionsWithKapt.arguments).toTypedArray()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user