From 681c70da4da8094306c98b666ac10bb9f46eb04a Mon Sep 17 00:00:00 2001 From: Phil Davies Date: Fri, 22 Nov 2024 18:45:25 +0000 Subject: [PATCH] feat: use java toolchain when generating SDL (#2058) ### :pencil: Description when the toolchain used by kotlin/java compilation tasks generates classfiles with newer versions, running the task in-process (i.e. with `classLoaderIsolation`) results in a build failing with UnsupportedClassVersionError. switching to `processIsolation` and using (by default) the configured java toolchain should mitigate this ### :link: Related Issues not raised (can raise an issue if required) --- .../gradle/tasks/GraphQLGenerateSDLTask.kt | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/tasks/GraphQLGenerateSDLTask.kt b/plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/tasks/GraphQLGenerateSDLTask.kt index 9f9c6116e1..de3bc51176 100644 --- a/plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/tasks/GraphQLGenerateSDLTask.kt +++ b/plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/tasks/GraphQLGenerateSDLTask.kt @@ -19,15 +19,20 @@ package com.expediagroup.graphql.plugin.gradle.tasks import com.expediagroup.graphql.plugin.gradle.actions.GenerateSDLAction import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.RegularFileProperty +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.Property import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Nested import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.SourceTask import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.options.Option -import org.gradle.workers.ClassLoaderWorkerSpec +import org.gradle.jvm.toolchain.JavaLauncher +import org.gradle.jvm.toolchain.JavaToolchainService +import org.gradle.workers.ProcessWorkerSpec import org.gradle.workers.WorkQueue import org.gradle.workers.WorkerExecutor import javax.inject.Inject @@ -57,14 +62,25 @@ abstract class GraphQLGenerateSDLTask : SourceTask() { @OutputFile val schemaFile: RegularFileProperty = project.objects.fileProperty() - @Inject - abstract fun getWorkerExecutor(): WorkerExecutor + @get:Inject + abstract val workerExecutor: WorkerExecutor + + @get:Inject + protected abstract val javaToolchainService: JavaToolchainService + + @get:Nested + abstract val launcher: Property init { group = "GraphQL" description = "Generate GraphQL schema in SDL format." schemaFile.convention(project.layout.buildDirectory.file("schema.graphql")) + + @Suppress("LeakingThis") + project.extensions.getByType(JavaPluginExtension::class.java).toolchain + .let(javaToolchainService::launcherFor) + .let(launcher::convention) } @TaskAction @@ -80,7 +96,12 @@ abstract class GraphQLGenerateSDLTask : SourceTask() { throw RuntimeException("failed to generate target schema directory = $targetDirectory") } - val workQueue: WorkQueue = getWorkerExecutor().classLoaderIsolation { workerSpec: ClassLoaderWorkerSpec -> + val workQueue: WorkQueue = workerExecutor.processIsolation { workerSpec: ProcessWorkerSpec -> + workerSpec.forkOptions { + it.setExecutable(launcher.get().executablePath.asFile) + } + logger.debug("worker executable: \n${workerSpec.forkOptions.executable}") + val workerClasspath = pluginClasspath.plus(projectClasspath).plus(source.files) workerSpec.classpath.from(workerClasspath) logger.debug("worker classpath: \n${workerSpec.classpath.files.joinToString("\n")}")