@@ -23,6 +23,7 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
23
23
import org.apache.commons.io.IOUtils
24
24
import org.apache.tools.ant.taskdefs.condition.Os
25
25
import org.elasticsearch.gradle.precommit.PrecommitTasks
26
+ import org.gradle.api.Action
26
27
import org.gradle.api.GradleException
27
28
import org.gradle.api.InvalidUserDataException
28
29
import org.gradle.api.JavaVersion
@@ -53,6 +54,7 @@ import org.gradle.api.tasks.javadoc.Javadoc
53
54
import org.gradle.internal.jvm.Jvm
54
55
import org.gradle.process.ExecResult
55
56
import org.gradle.process.ExecSpec
57
+ import org.gradle.process.internal.ExecException
56
58
import org.gradle.util.GradleVersion
57
59
58
60
import java.nio.charset.StandardCharsets
@@ -155,8 +157,8 @@ class BuildPlugin implements Plugin<Project> {
155
157
runtimeJavaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, runtimeJavaHome))
156
158
}
157
159
158
- String inFipsJvmScript = ' print( java.security.Security.getProviders()[0].name.toLowerCase().contains("fips")); '
159
- boolean inFipsJvm = Boolean . parseBoolean(runJavaAsScript (project, runtimeJavaHome, inFipsJvmScript))
160
+ String inFipsJvmScript = ' java.security.Security.getProviders()[0].name.toLowerCase().contains("fips")'
161
+ boolean inFipsJvm = Boolean . parseBoolean(runScript (project, runtimeJavaHome, inFipsJvmScript))
160
162
161
163
// Build debugging info
162
164
println ' ======================================='
@@ -446,55 +448,62 @@ class BuildPlugin implements Plugin<Project> {
446
448
447
449
/* * Finds printable java version of the given JAVA_HOME */
448
450
private static String findJavaVersionDetails (Project project , String javaHome ) {
449
- String versionInfoScript = ' print(' +
450
- ' java.lang.System.getProperty("java.vendor.version", java.lang.System.getProperty("java.vendor")) + " " + ' +
451
+ String versionInfoScript = ' java.lang.System.getProperty("java.vendor.version", java.lang.System.getProperty("java.vendor")) + " " + ' +
451
452
' java.lang.System.getProperty("java.version") + " [" +' +
452
453
' java.lang.System.getProperty("java.vm.name") + " " + ' +
453
- ' java.lang.System.getProperty("java.vm.version") + "]"); '
454
- return runJavaAsScript (project, javaHome, versionInfoScript). trim()
454
+ ' java.lang.System.getProperty("java.vm.version") + "]"'
455
+ return runScript (project, javaHome, versionInfoScript). trim()
455
456
}
456
457
457
458
/* * Finds the parsable java specification version */
458
459
private static String findJavaSpecificationVersion (Project project , String javaHome ) {
459
- String versionScript = ' print(java.lang.System.getProperty("java.specification.version"));'
460
- return runJavaAsScript(project, javaHome, versionScript)
461
- }
462
-
463
- private static String findJavaVendor (Project project , String javaHome ) {
464
- String vendorScript = ' print(java.lang.System.getProperty("java.vendor.version", System.getProperty("java.vendor"));'
465
- return runJavaAsScript(project, javaHome, vendorScript)
466
- }
467
-
468
- /* * Finds the parsable java specification version */
469
- private static String findJavaVersion (Project project , String javaHome ) {
470
- String versionScript = ' print(java.lang.System.getProperty("java.version"));'
471
- return runJavaAsScript(project, javaHome, versionScript)
460
+ String versionScript = ' java.lang.System.getProperty("java.specification.version")'
461
+ return runScript(project, javaHome, versionScript)
472
462
}
473
463
474
464
/* * Runs the given javascript using jjs from the jdk, and returns the output */
475
- private static String runJavaAsScript (Project project , String javaHome , String script ) {
476
- ByteArrayOutputStream stdout = new ByteArrayOutputStream ()
477
- ByteArrayOutputStream stderr = new ByteArrayOutputStream ()
465
+ private static String runScript (Project project , String javaHome , String script ) {
478
466
if (Os . isFamily(Os . FAMILY_WINDOWS )) {
479
467
// gradle/groovy does not properly escape the double quote for windows
480
468
script = script. replace(' "' , ' \\ "' )
481
469
}
482
- File jrunscriptPath = new File (javaHome, ' bin/jrunscript' )
483
- ExecResult result = project. exec {
484
- executable = jrunscriptPath
485
- args ' -e' , script
486
- standardOutput = stdout
487
- errorOutput = stderr
488
- ignoreExitValue = true
470
+
471
+ try {
472
+ File jrunscriptPath = new File (javaHome, ' bin/jrunscript' )
473
+ return exec(project, false ) { spec ->
474
+ spec. executable = jrunscriptPath
475
+ spec. args ' -e' , " print($script );"
476
+ }
477
+ } catch (ExecException ex) {
478
+ // if jrunscript fails, let's try jshell
479
+ ByteArrayInputStream input = new ByteArrayInputStream (" System.err.print(${ script} );\n " . getBytes(' UTF-8' ))
480
+ File jshellPath = new File (javaHome, ' bin/jshell' )
481
+ return exec(project, true ) { spec ->
482
+ spec. executable = jshellPath
483
+ spec. args ' -s'
484
+ spec. standardInput = input
485
+ }
486
+ }
487
+ }
488
+
489
+ private static String exec (Project project , boolean captureStdErr , Action<? super ExecSpec > spec ) {
490
+ ByteArrayOutputStream stdout = new ByteArrayOutputStream ()
491
+ ByteArrayOutputStream stderr = new ByteArrayOutputStream ()
492
+ ExecResult result = project. exec { ExecSpec s ->
493
+ spec. execute(s)
494
+ s. standardOutput = stdout
495
+ s. errorOutput = stderr
496
+ s. ignoreExitValue = true
489
497
}
490
498
if (result. exitValue != 0 ) {
491
499
project. logger. error(" STDOUT:" )
492
500
stdout. toString(' UTF-8' ). eachLine { line -> project. logger. error(line) }
493
501
project. logger. error(" STDERR:" )
494
502
stderr. toString(' UTF-8' ). eachLine { line -> project. logger. error(line) }
495
503
result. rethrowFailure()
504
+ result. assertNormalExitValue() // assert exit value in case the failure cause is null
496
505
}
497
- return stdout. toString(' UTF-8' ). trim()
506
+ return (captureStdErr ? stderr : stdout) . toString(' UTF-8' ). trim()
498
507
}
499
508
500
509
/* * Return the configuration name used for finding transitive deps of the given dependency. */
0 commit comments