Skip to content

Commit 4110030

Browse files
committed
Backport #849
1 parent ad6c50b commit 4110030

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

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

+43-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
* Bachmann electrontic GmbH - 510425 parallel mode requires threadCount>1 or useUnlimitedThreads=true
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
17+
* - [Issue 790] Support printing of bundle wirings in tycho-surefire-plugin
18+
* - [Issue 849] JAVA_HOME check is not OS independent
19+
* - [Issue 790] Support printing of bundle wirings in tycho-surefire-plugin
1720
******************************************************************************/
1821
package org.eclipse.tycho.surefire;
1922

2023
import java.io.BufferedOutputStream;
2124
import java.io.File;
25+
import java.io.FileFilter;
2226
import java.io.FileOutputStream;
2327
import java.io.IOException;
2428
import java.net.MalformedURLException;
@@ -34,6 +38,7 @@
3438
import java.util.Set;
3539
import java.util.StringJoiner;
3640

41+
import org.apache.commons.io.FilenameUtils;
3742
import org.apache.maven.artifact.Artifact;
3843
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
3944
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
@@ -102,6 +107,8 @@
102107

103108
public abstract class AbstractTestMojo extends AbstractMojo {
104109

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

111118
private static final Object LOCK = new Object();
112119

120+
private static final String[] JAVA_EXECUTABLES = { "java", "java.exe" };
121+
113122
/**
114123
* Root directory (<a href=
115124
* "https://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/misc/runtime-options.html#osgiinstallarea"
@@ -1127,7 +1136,7 @@ private String decodeReturnCode(int result) {
11271136
protected Toolchain getToolchain() throws MojoExecutionException {
11281137
if (JDKUsage.SYSTEM.equals(useJDK)) {
11291138
if (toolchainManager != null) {
1130-
return toolchainManager.getToolchainFromBuildContext("jdk", session);
1139+
return toolchainManager.getToolchainFromBuildContext(SYSTEM_JDK, session);
11311140
}
11321141
return null;
11331142
}
@@ -1209,16 +1218,44 @@ protected String getJavaExecutable() throws MojoExecutionException {
12091218
}
12101219
String javaHome = System.getenv("JAVA_HOME");
12111220
if (javaHome != null && !javaHome.isBlank()) {
1212-
File file = new File(javaHome, "bin/java");
1213-
if (file.exists()) {
1214-
getLog().info("Could not find the Toolchain, using java from JAVA_HOME instead");
1215-
return file.getAbsolutePath();
1221+
File java = getJavaFromJavaHome(javaHome);
1222+
if (java != null) {
1223+
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
1224+
+ ", using java from JAVA_HOME instead (" + java.getAbsolutePath() + ")");
1225+
return java.getAbsolutePath();
12161226
}
1227+
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
1228+
+ " and JAVA_HOME seem to not point to a valid location, trying java from PATH instead (current JAVA_HOME="
1229+
+ javaHome + ")");
1230+
} else {
1231+
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
1232+
+ " and JAVA_HOME is not set, trying java from PATH instead");
12171233
}
1218-
getLog().info("Could not find the Toolchain nor JAVA_HOME, trying java from PATH instead");
12191234
return "java";
12201235
}
12211236

1237+
private File getJavaFromJavaHome(String javaHome) {
1238+
File javaBin = new File(javaHome, "bin");
1239+
for (String executable : JAVA_EXECUTABLES) {
1240+
File java = new File(javaBin, executable);
1241+
if (java.isFile()) {
1242+
return java;
1243+
}
1244+
}
1245+
//last resort just in case other extension or case-sensitive file-system...
1246+
File[] listFiles = javaBin.listFiles(new FileFilter() {
1247+
1248+
@Override
1249+
public boolean accept(File pathname) {
1250+
return pathname.isFile() && FilenameUtils.getBaseName(pathname.getName().toLowerCase()).equals("java");
1251+
}
1252+
});
1253+
if (listFiles != null && listFiles.length > 0) {
1254+
return listFiles[0];
1255+
}
1256+
return null;
1257+
}
1258+
12221259
private Map<String, String> getMergedSystemProperties() {
12231260
Map<String, String> result = new LinkedHashMap<>();
12241261
// bug 415489: use osgi.clean=true by default

0 commit comments

Comments
 (0)