in src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java [258:313]
ToolchainModel doGetToolchainModel(Path jdk) {
Path java = jdk.resolve("bin").resolve("java");
if (!Files.exists(java)) {
java = jdk.resolve("bin").resolve("java.exe");
if (!Files.exists(java)) {
log.debug("JDK toolchain discovered at " + jdk
+ " will be ignored: unable to find bin/java or bin\\java.exe");
return null;
}
}
if (!java.toFile().canExecute()) {
log.debug("JDK toolchain discovered at " + jdk
+ " will be ignored: the bin/java or bin\\java.exe is not executable");
return null;
}
List<String> lines;
try {
Path temp = Files.createTempFile("jdk-opts-", ".out");
try {
new ProcessBuilder()
.command(java.toString(), "-XshowSettings:properties", "-version")
.redirectError(temp.toFile())
.start()
.waitFor();
lines = Files.readAllLines(temp);
} finally {
Files.delete(temp);
}
} catch (IOException | InterruptedException e) {
log.debug("JDK toolchain discovered at " + jdk + " will be ignored: error executing java: " + e);
return null;
}
Map<String, String> properties = new LinkedHashMap<>();
Stream.of(PROPERTIES).forEach(name -> {
lines.stream()
.filter(l -> l.contains(JAVA + name))
.map(l -> l.replaceFirst(".*=\\s*(.*)", "$1"))
.findFirst()
.ifPresent(value -> properties.put(name, value));
});
if (!properties.containsKey(VERSION)) {
log.debug("JDK toolchain discovered at " + jdk + " will be ignored: could not obtain " + JAVA + VERSION);
return null;
}
ToolchainModel model = new ToolchainModel();
model.setType(TOOLCHAIN_TYPE_JDK);
properties.forEach(model::addProvide);
Xpp3Dom configuration = new Xpp3Dom("configuration");
Xpp3Dom jdkHome = new Xpp3Dom(JDK_HOME);
jdkHome.setValue(jdk.toString());
configuration.addChild(jdkHome);
model.setConfiguration(configuration);
return model;
}