mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-10 13:15:27 +00:00
Calculate java home from java command (#716)
This commit is contained in:
@@ -44,6 +44,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
|||||||
import org.mvndaemon.mvnd.common.Environment;
|
import org.mvndaemon.mvnd.common.Environment;
|
||||||
import org.mvndaemon.mvnd.common.InterpolationHelper;
|
import org.mvndaemon.mvnd.common.InterpolationHelper;
|
||||||
import org.mvndaemon.mvnd.common.Os;
|
import org.mvndaemon.mvnd.common.Os;
|
||||||
|
import org.mvndaemon.mvnd.common.OsUtils;
|
||||||
import org.mvndaemon.mvnd.common.SocketFamily;
|
import org.mvndaemon.mvnd.common.SocketFamily;
|
||||||
import org.mvndaemon.mvnd.common.TimeUtils;
|
import org.mvndaemon.mvnd.common.TimeUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -137,6 +138,9 @@ public class DaemonParameters {
|
|||||||
.orLocalProperty(provider, globalPropertiesPath())
|
.orLocalProperty(provider, globalPropertiesPath())
|
||||||
.orSystemProperty()
|
.orSystemProperty()
|
||||||
.orEnvironmentVariable()
|
.orEnvironmentVariable()
|
||||||
|
.or(new ValueSource(
|
||||||
|
description -> description.append("java command"),
|
||||||
|
this::javaHomeFromPath))
|
||||||
.orFail()
|
.orFail()
|
||||||
.asPath();
|
.asPath();
|
||||||
try {
|
try {
|
||||||
@@ -146,6 +150,14 @@ public class DaemonParameters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String javaHomeFromPath() {
|
||||||
|
final String jHome = OsUtils.findJavaHomeFromPath();
|
||||||
|
if (null != jHome) {
|
||||||
|
System.setProperty(Environment.JAVA_HOME.getProperty(), jHome);
|
||||||
|
}
|
||||||
|
return jHome;
|
||||||
|
}
|
||||||
|
|
||||||
public Path userDir() {
|
public Path userDir() {
|
||||||
return value(Environment.USER_DIR)
|
return value(Environment.USER_DIR)
|
||||||
.orSystemProperty()
|
.orSystemProperty()
|
||||||
|
@@ -101,6 +101,18 @@ public class OsUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String findJavaHomeFromPath() {
|
||||||
|
String[] cmd = { "java", "-XshowSettings:properties", "-version" };
|
||||||
|
final List<String> output = new ArrayList<String>(1);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
private static void exec(String[] cmd, final List<String> output) {
|
private static void exec(String[] cmd, final List<String> output) {
|
||||||
final ProcessBuilder builder = new ProcessBuilder(cmd).redirectErrorStream(true);
|
final ProcessBuilder builder = new ProcessBuilder(cmd).redirectErrorStream(true);
|
||||||
try (CommandProcess ps = new CommandProcess(builder.start(), output::add)) {
|
try (CommandProcess ps = new CommandProcess(builder.start(), output::add)) {
|
||||||
|
Reference in New Issue
Block a user