From 95f8f0d9a50f1cfc3b6ff2499ac75c2252c9d8c0 Mon Sep 17 00:00:00 2001 From: Sergii Kondrusiev Date: Mon, 13 May 2024 15:54:13 +0300 Subject: [PATCH] Make Gretty java toolchain aware --- .../org/akhikhl/gretty/JavaExecParams.groovy | 6 +++++ .../org/akhikhl/gretty/LauncherBase.groovy | 1 + .../org/akhikhl/gretty/ServerConfig.groovy | 6 ++++- .../org/akhikhl/gretty/StarterLauncher.groovy | 10 ++++--- .../org/akhikhl/gretty/DefaultLauncher.groovy | 5 ++++ .../org/akhikhl/gretty/StartBaseTask.groovy | 27 +++++++++++++++++++ .../gretty/TaskWithServerConfig.groovy | 4 +++ 7 files changed, 55 insertions(+), 4 deletions(-) diff --git a/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/JavaExecParams.groovy b/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/JavaExecParams.groovy index 7a3c7b69f..6ca0ddefa 100644 --- a/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/JavaExecParams.groovy +++ b/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/JavaExecParams.groovy @@ -19,6 +19,8 @@ class JavaExecParams { String main + String jvmExecutable + List jvmArgs = [] List args = [] @@ -29,6 +31,10 @@ class JavaExecParams { Map systemProperties = [:] + void jvmExecutable(String jvmExecutable) { + this.jvmExecutable = jvmExecutable + } + void arg(String a) { args.add(a) } diff --git a/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/LauncherBase.groovy b/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/LauncherBase.groovy index f64c6d13a..4e86c3020 100644 --- a/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/LauncherBase.groovy +++ b/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/LauncherBase.groovy @@ -188,6 +188,7 @@ abstract class LauncherBase implements Launcher { params.debug = config.getDebug() params.debugSuspend = config.getDebugSuspend() params.debugPort = config.getDebugPort() + params.jvmExecutable = sconfig.jvmExecutable params.jvmArgs = sconfig.jvmArgs params.systemProperties = sconfig.systemProperties if(!sconfig.secureRandom) { diff --git a/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/ServerConfig.groovy b/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/ServerConfig.groovy index 68cf87829..9d6d8af3d 100644 --- a/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/ServerConfig.groovy +++ b/libs/gretty-core/src/main/groovy/org/akhikhl/gretty/ServerConfig.groovy @@ -21,7 +21,7 @@ import groovy.transform.TypeCheckingMode class ServerConfig { static final int RANDOM_FREE_PORT = -1 - + String jvmExecutable List jvmArgs Map systemProperties String servletContainer @@ -161,4 +161,8 @@ class ServerConfig { systemProperties << m } } + + void jvmExecutable(String jvmExecutable) { + this.jvmExecutable = jvmExecutable + } } diff --git a/libs/gretty-starter/src/main/groovy/org/akhikhl/gretty/StarterLauncher.groovy b/libs/gretty-starter/src/main/groovy/org/akhikhl/gretty/StarterLauncher.groovy index b3567eb36..a9bee3cfb 100644 --- a/libs/gretty-starter/src/main/groovy/org/akhikhl/gretty/StarterLauncher.groovy +++ b/libs/gretty-starter/src/main/groovy/org/akhikhl/gretty/StarterLauncher.groovy @@ -57,13 +57,17 @@ class StarterLauncher extends LauncherBase { @Override protected void javaExec(JavaExecParams params) { - String javaExe = PlatformUtils.isWindows() ? 'java.exe' : 'java' - String javaPath = new File(System.getProperty("java.home"), "bin/$javaExe").absolutePath + String jvmExecutable = Optional.ofNullable(params.jvmExecutable).orElseGet({ + String javaExe = PlatformUtils.isWindows() ? 'java.exe' : 'java' + String javaPath = new File(System.getProperty("java.home"), "bin/$javaExe").absolutePath + return javaPath + }) + def classPath = [ new File(basedir, 'runner/*') ] classPath = classPath.collect { it.absolutePath }.join(System.getProperty('path.separator')) // Note that JavaExecParams debugging properties are intentionally ignored. // It is supposed that webapp debugging is performed via DefaultLauncher. - def procParams = [ javaPath ] + params.jvmArgs + ['-DgrettyProduct=true'] + params.systemProperties.collect { k, v -> "-D$k=$v" } + [ '-cp', classPath, params.main ] + params.args + def procParams = [ jvmExecutable ] + params.jvmArgs + ['-DgrettyProduct=true'] + params.systemProperties.collect { k, v -> "-D$k=$v" } + [ '-cp', classPath, params.main ] + params.args log.debug 'Launching runner process: {}', procParams.join(' ') Process proc = procParams.execute() proc.waitForProcessOutput(System.out, System.err) diff --git a/libs/gretty/src/main/groovy/org/akhikhl/gretty/DefaultLauncher.groovy b/libs/gretty/src/main/groovy/org/akhikhl/gretty/DefaultLauncher.groovy index 6497e1764..6b0bc588f 100644 --- a/libs/gretty/src/main/groovy/org/akhikhl/gretty/DefaultLauncher.groovy +++ b/libs/gretty/src/main/groovy/org/akhikhl/gretty/DefaultLauncher.groovy @@ -112,6 +112,11 @@ class DefaultLauncher extends LauncherBase { } log.info 'DEBUG MODE, port={}, suspend={}', params.debugPort, params.debugSuspend } + + if (params.jvmExecutable) { + spec.executable params.jvmExecutable + } + spec.jvmArgs jvmArgs spec.systemProperties params.systemProperties spec.mainClass.set(params.main) diff --git a/libs/gretty/src/main/groovy/org/akhikhl/gretty/StartBaseTask.groovy b/libs/gretty/src/main/groovy/org/akhikhl/gretty/StartBaseTask.groovy index d4d67d4b0..a27c445df 100644 --- a/libs/gretty/src/main/groovy/org/akhikhl/gretty/StartBaseTask.groovy +++ b/libs/gretty/src/main/groovy/org/akhikhl/gretty/StartBaseTask.groovy @@ -14,8 +14,12 @@ import org.akhikhl.gretty.scanner.JDKScannerManager import org.gradle.api.DefaultTask import org.gradle.api.Task import org.gradle.api.plugins.ExtensionAware +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction +import org.gradle.jvm.toolchain.JavaLauncher +import org.gradle.jvm.toolchain.JavaToolchainService +import org.gradle.jvm.toolchain.JavaToolchainSpec import org.gradle.process.JavaForkOptions import org.gradle.testing.jacoco.plugins.JacocoTaskExtension /** @@ -115,6 +119,10 @@ abstract class StartBaseTask extends DefaultTask { sconfig.systemProperty 'springloaded', 'exclusions=org.akhikhl.gretty..*' } + getJavaToolchainJvmExecutable().ifPresent({ jvmExecutable -> + sconfig.jvmExecutable(jvmExecutable) + }) + for(Closure c in prepareServerConfigClosures) { c = c.rehydrate(sconfig, c.owner, c.thisObject) c.resolveStrategy = Closure.DELEGATE_FIRST @@ -165,6 +173,25 @@ abstract class StartBaseTask extends DefaultTask { } } + private Optional getJavaToolchainLauncher() { + Optional toolchainService = Optional.ofNullable(project.extensions.getByType(JavaToolchainService)) + Optional launcher = Optional.ofNullable(project.extensions.getByType(JavaPluginExtension)) + .map({ it.getToolchain() }) + .flatMap({ JavaToolchainSpec spec -> + toolchainService + .map({ it.launcherFor(spec) }) + .map({ it.getOrElse(null) }) + }) + + return launcher + } + + private Optional getJavaToolchainJvmExecutable() { + return getJavaToolchainLauncher() + .map({ it.executablePath?.asFile?.path }) + } + + @Internal protected final LauncherConfig getLauncherConfig() { diff --git a/libs/gretty/src/main/groovy/org/akhikhl/gretty/TaskWithServerConfig.groovy b/libs/gretty/src/main/groovy/org/akhikhl/gretty/TaskWithServerConfig.groovy index 4981d79fe..a92a54dd3 100644 --- a/libs/gretty/src/main/groovy/org/akhikhl/gretty/TaskWithServerConfig.groovy +++ b/libs/gretty/src/main/groovy/org/akhikhl/gretty/TaskWithServerConfig.groovy @@ -10,6 +10,10 @@ import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity interface TaskWithServerConfig { + @Optional + @Input + String getJvmExecutable() + @Optional @Input List getJvmArgs()