Skip to content
/ tycho Public
forked from eclipse-tycho/tycho

Commit ced6c2d

Browse files
committedApr 6, 2022
Fix eclipse-tycho#849 JAVA_HOME check is not OS independent
1 parent f69b9e1 commit ced6c2d

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed
 

‎tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractTestMojo.java

+41-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* Christoph Läubrich - [Bug 529929] improve error message in case of failures
1616
* - [Bug 572420] Tycho-Surefire should be executable for eclipse-plugin package type
1717
* - [Issue 790] Support printing of bundle wirings in tycho-surefire-plugin
18+
* - [Issue 849] JAVA_HOME check is not OS independent
1819
******************************************************************************/
1920
package org.eclipse.tycho.surefire;
2021

2122
import java.io.BufferedOutputStream;
2223
import java.io.File;
24+
import java.io.FileFilter;
2325
import java.io.FileOutputStream;
2426
import java.io.IOException;
2527
import java.net.MalformedURLException;
@@ -35,6 +37,7 @@
3537
import java.util.Set;
3638
import java.util.StringJoiner;
3739

40+
import org.apache.commons.io.FilenameUtils;
3841
import org.apache.maven.artifact.Artifact;
3942
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
4043
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
@@ -103,6 +106,8 @@
103106

104107
public abstract class AbstractTestMojo extends AbstractMojo {
105108

109+
private static final String SYSTEM_JDK = "jdk";
110+
106111
private static String[] UNIX_SIGNAL_NAMES = { "not a signal", // padding, signals start with 1
107112
"SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL", "SIGUSR1",
108113
"SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP",
@@ -111,6 +116,8 @@ public abstract class AbstractTestMojo extends AbstractMojo {
111116

112117
private static final ConcurrencyLock CONCURRENCY_LOCK = new ConcurrencyLock();
113118

119+
private static final String[] JAVA_EXECUTABLES = { "java", "java.exe" };
120+
114121
/**
115122
* Root directory (<a href=
116123
* "https://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/misc/runtime-options.html#osgiinstallarea"
@@ -1152,7 +1159,7 @@ private String decodeReturnCode(int result) {
11521159
protected Toolchain getToolchain() throws MojoExecutionException {
11531160
if (JDKUsage.SYSTEM.equals(useJDK)) {
11541161
if (toolchainManager != null) {
1155-
return toolchainManager.getToolchainFromBuildContext("jdk", session);
1162+
return toolchainManager.getToolchainFromBuildContext(SYSTEM_JDK, session);
11561163
}
11571164
return null;
11581165
}
@@ -1234,16 +1241,44 @@ protected String getJavaExecutable() throws MojoExecutionException {
12341241
}
12351242
String javaHome = System.getenv("JAVA_HOME");
12361243
if (javaHome != null && !javaHome.isBlank()) {
1237-
File file = new File(javaHome, "bin/java");
1238-
if (file.exists()) {
1239-
getLog().info("Could not find the Toolchain, using java from JAVA_HOME instead");
1240-
return file.getAbsolutePath();
1244+
File java = getJavaFromJavaHome(javaHome);
1245+
if (java != null) {
1246+
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
1247+
+ ", using java from JAVA_HOME instead (" + java.getAbsolutePath() + ")");
1248+
return java.getAbsolutePath();
12411249
}
1250+
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
1251+
+ " and JAVA_HOME seem to not point to a valid location, trying java from PATH instead (current JAVA_HOME="
1252+
+ javaHome + ")");
1253+
} else {
1254+
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
1255+
+ " and JAVA_HOME is not set, trying java from PATH instead");
12421256
}
1243-
getLog().info("Could not find the Toolchain nor JAVA_HOME, trying java from PATH instead");
12441257
return "java";
12451258
}
12461259

1260+
private File getJavaFromJavaHome(String javaHome) {
1261+
File javaBin = new File(javaHome, "bin");
1262+
for (String executable : JAVA_EXECUTABLES) {
1263+
File java = new File(javaBin, executable);
1264+
if (java.isFile()) {
1265+
return java;
1266+
}
1267+
}
1268+
//last resort just in case other extension or case-sensitive file-system...
1269+
File[] listFiles = javaBin.listFiles(new FileFilter() {
1270+
1271+
@Override
1272+
public boolean accept(File pathname) {
1273+
return pathname.isFile() && FilenameUtils.getBaseName(pathname.getName().toLowerCase()).equals("java");
1274+
}
1275+
});
1276+
if (listFiles != null && listFiles.length > 0) {
1277+
return listFiles[0];
1278+
}
1279+
return null;
1280+
}
1281+
12471282
private Map<String, String> getMergedSystemProperties() {
12481283
Map<String, String> result = new LinkedHashMap<>();
12491284
// bug 415489: use osgi.clean=true by default

0 commit comments

Comments
 (0)