diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintBasePlugin.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintBasePlugin.kt index 2aab6c58..1806ec37 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintBasePlugin.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintBasePlugin.kt @@ -63,6 +63,6 @@ open class KtlintBasePlugin : Plugin { } companion object { - const val LOWEST_SUPPORTED_GRADLE_VERSION = "6.0" + const val LOWEST_SUPPORTED_GRADLE_VERSION = "6.7" } } diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt index ac8601e2..285b563a 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt @@ -10,6 +10,8 @@ import org.gradle.api.file.ProjectLayout import org.gradle.api.file.RegularFileProperty import org.gradle.api.model.ObjectFactory import org.gradle.api.model.ReplacedBy +import org.gradle.api.plugins.JavaBasePlugin +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.provider.Property import org.gradle.api.provider.SetProperty import org.gradle.api.tasks.Classpath @@ -17,11 +19,15 @@ import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.Nested +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.SourceTask +import org.gradle.jvm.toolchain.JavaLauncher +import org.gradle.jvm.toolchain.JavaToolchainService import org.gradle.work.ChangeType import org.gradle.work.Incremental import org.gradle.work.InputChanges @@ -43,6 +49,10 @@ abstract class BaseKtLintCheckTask @Inject constructor( private val workerExecutor: WorkerExecutor, ) : SourceTask() { + @get:Nested + @get:Optional + abstract val launcher: Property + @get:Classpath internal abstract val ktLintClasspath: ConfigurableFileCollection @@ -95,6 +105,14 @@ abstract class BaseKtLintCheckTask @Inject constructor( include("**/*.$it") } } + + project.plugins.withType(JavaBasePlugin::class.java).configureEach { + val toolchain = project.extensions.getByType(JavaPluginExtension::class.java).toolchain + + val service = project.extensions.getByType(JavaToolchainService::class.java) + val defaultLauncher = service.launcherFor(toolchain) + launcher.convention(defaultLauncher) + } } @ReplacedBy("stableSources") @@ -150,6 +168,9 @@ abstract class BaseKtLintCheckTask @Inject constructor( spec.classpath.from(ktLintClasspath, ruleSetsClasspath) spec.forkOptions { options -> options.maxHeapSize = workerMaxHeapSize.get() + if(launcher.isPresent) { + options.executable = launcher.get().executablePath.asFile.absolutePath + } } } diff --git a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPluginTest.kt b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPluginTest.kt index 1e4f771c..f6d34eb6 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPluginTest.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPluginTest.kt @@ -480,4 +480,40 @@ abstract class BaseKtlintPluginTest : AbstractPluginTest() { assertThat(output).contains("com.pinterest.ktlint:ktlint-core:0.34.2 -> 0.41.0") } } + + @Test + internal fun `Should use JVM Toolchains`() { + val initialSourceFile = "src/main/kotlin/initial.kt" + projectRoot.createSourceFile( + initialSourceFile, + """ + val foo = "bar" + + """.trimIndent() + ) + + + projectRoot.buildFile().appendText( + """ + + java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(16)) + } + } + + tasks.withType(org.jlleitschuh.gradle.ktlint.tasks.KtLintCheckTask) { + doLast { + project.logger.lifecycle("Running ktlint task with JDK ${'$'}{it.launcher.get().metadata.languageVersion.asInt()}") + } + } + """.trimIndent() + ) + + build(CHECK_PARENT_TASK_NAME).apply { + assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(output.contains("Running ktlint task with JDK 16")) + } + + } }