Skip to content

Commit

Permalink
Make Gretty java toolchain aware
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-serjey committed May 13, 2024
1 parent c69f1dc commit 95f8f0d
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class JavaExecParams {

String main

String jvmExecutable

List<String> jvmArgs = []

List<String> args = []
Expand All @@ -29,6 +31,10 @@ class JavaExecParams {

Map<String, String> systemProperties = [:]

void jvmExecutable(String jvmExecutable) {
this.jvmExecutable = jvmExecutable
}

void arg(String a) {
args.add(a)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import groovy.transform.TypeCheckingMode
class ServerConfig {

static final int RANDOM_FREE_PORT = -1

String jvmExecutable
List<String> jvmArgs
Map<String, String> systemProperties
String servletContainer
Expand Down Expand Up @@ -161,4 +161,8 @@ class ServerConfig {
systemProperties << m
}
}

void jvmExecutable(String jvmExecutable) {
this.jvmExecutable = jvmExecutable
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -165,6 +173,25 @@ abstract class StartBaseTask extends DefaultTask {
}
}

private Optional<JavaLauncher> getJavaToolchainLauncher() {
Optional<JavaToolchainService> toolchainService = Optional.ofNullable(project.extensions.getByType(JavaToolchainService))
Optional<JavaLauncher> 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<String> getJavaToolchainJvmExecutable() {
return getJavaToolchainLauncher()
.map({ it.executablePath?.asFile?.path })
}


@Internal
protected final LauncherConfig getLauncherConfig() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getJvmArgs()
Expand Down

0 comments on commit 95f8f0d

Please sign in to comment.