Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle Toolchain support #513

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ open class KtlintBasePlugin : Plugin<Project> {
}

companion object {
const val LOWEST_SUPPORTED_GRADLE_VERSION = "6.0"
const val LOWEST_SUPPORTED_GRADLE_VERSION = "6.7"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ 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
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
Expand All @@ -43,6 +49,10 @@ abstract class BaseKtLintCheckTask @Inject constructor(
private val workerExecutor: WorkerExecutor,
) : SourceTask() {

@get:Nested
@get:Optional
abstract val launcher: Property<JavaLauncher>

@get:Classpath
internal abstract val ktLintClasspath: ConfigurableFileCollection

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

}
}