Skip to content

Commit

Permalink
feat: use java toolchain when generating SDL (#2058)
Browse files Browse the repository at this point in the history
### 📝 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

### 🔗 Related Issues

not raised (can raise an issue if required)
  • Loading branch information
tKe authored Nov 22, 2024
1 parent 700f0ea commit 681c70d
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<JavaLauncher>

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

0 comments on commit 681c70d

Please sign in to comment.