mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-10 01:19:11 +00:00
* Add a test * Simplify the stream processing Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
This commit is contained in:
@@ -139,7 +139,8 @@ public class DaemonParameters {
|
||||
.orLocalProperty(provider, globalPropertiesPath())
|
||||
.orSystemProperty()
|
||||
.orEnvironmentVariable()
|
||||
.or(new ValueSource(description -> description.append("java command"), this::javaHomeFromPath))
|
||||
.or(new ValueSource(
|
||||
description -> description.append("java command"), DaemonParameters::javaHomeFromPath))
|
||||
.orFail()
|
||||
.asPath();
|
||||
try {
|
||||
@@ -149,8 +150,12 @@ public class DaemonParameters {
|
||||
}
|
||||
}
|
||||
|
||||
private String javaHomeFromPath() {
|
||||
final String jHome = OsUtils.findJavaHomeFromPath();
|
||||
private static String javaHomeFromPath() {
|
||||
LOG.warn(
|
||||
"Falling back to finding JAVA_HOME by running java executable available in PATH."
|
||||
+ " You may want to avoid this time consumig task by setting JAVA_HOME environment variable"
|
||||
+ " or by passing java.home system property through command line or in one of mvnd configuration files.");
|
||||
final String jHome = OsUtils.findJavaHomeFromJavaExecutable("java");
|
||||
if (null != jHome) {
|
||||
System.setProperty(Environment.JAVA_HOME.getProperty(), jHome);
|
||||
}
|
||||
|
@@ -111,16 +111,23 @@ public class OsUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static String findJavaHomeFromPath() {
|
||||
String[] cmd = {"java", "-XshowSettings:properties", "-version"};
|
||||
final List<String> output = new ArrayList<String>(1);
|
||||
/**
|
||||
* Executes the given {@code javaExecutable} with {@code -XshowSettings:properties -version} parameters and extracts
|
||||
* the value of {@code java.home} from the output.
|
||||
*
|
||||
* @param javaExecutable pass {@code "java"} to get {@code java} binary available in {@code PATH} environment
|
||||
* variable or pass an absolute path to a {@code "java"} executable
|
||||
* @return a {@code java.home} value or null
|
||||
*/
|
||||
public static String findJavaHomeFromJavaExecutable(String javaExecutable) {
|
||||
String[] cmd = {javaExecutable, "-XshowSettings:properties", "-version"};
|
||||
final List<String> output = new ArrayList<String>();
|
||||
exec(cmd, output);
|
||||
List<String> javaHomeLines =
|
||||
output.stream().filter(l -> l.contains(" java.home = ")).collect(Collectors.toList());
|
||||
if (javaHomeLines.size() == 1) {
|
||||
return javaHomeLines.get(0).trim().replaceFirst("java.home = ", "");
|
||||
}
|
||||
return null;
|
||||
return output.stream()
|
||||
.filter(l -> l.contains(" java.home = "))
|
||||
.map(l -> l.substring(l.indexOf('=') + 1).trim())
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private static void exec(String[] cmd, final List<String> output) {
|
||||
|
@@ -31,4 +31,11 @@ public class OsUtilsTest {
|
||||
Assertions.assertEquals("1g", OsUtils.kbTohumanReadable(1024 * 1024));
|
||||
Assertions.assertEquals("1t", OsUtils.kbTohumanReadable(1024 * 1024 * 1024));
|
||||
}
|
||||
|
||||
@Test
|
||||
void findJavaHomeFromPath() {
|
||||
final String expectedJavaHome = System.getProperty("java.home");
|
||||
Assertions.assertEquals(
|
||||
expectedJavaHome, OsUtils.findJavaHomeFromJavaExecutable(expectedJavaHome + "/bin/java"));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user