Skip to content

Commit

Permalink
Backport #849
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Apr 6, 2022
1 parent ad6c50b commit 4110030
Showing 1 changed file with 43 additions and 6 deletions.
Expand Up @@ -14,11 +14,15 @@
* Bachmann electrontic GmbH - 510425 parallel mode requires threadCount>1 or useUnlimitedThreads=true
* Christoph Läubrich - [Bug 529929] improve error message in case of failures
* - [Bug 572420] Tycho-Surefire should be executable for eclipse-plugin package type
* - [Issue 790] Support printing of bundle wirings in tycho-surefire-plugin
* - [Issue 849] JAVA_HOME check is not OS independent
* - [Issue 790] Support printing of bundle wirings in tycho-surefire-plugin
******************************************************************************/
package org.eclipse.tycho.surefire;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -34,6 +38,7 @@
import java.util.Set;
import java.util.StringJoiner;

import org.apache.commons.io.FilenameUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
Expand Down Expand Up @@ -102,6 +107,8 @@

public abstract class AbstractTestMojo extends AbstractMojo {

private static final String SYSTEM_JDK = "jdk";

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

private static final Object LOCK = new Object();

private static final String[] JAVA_EXECUTABLES = { "java", "java.exe" };

/**
* Root directory (<a href=
* "https://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/misc/runtime-options.html#osgiinstallarea"
Expand Down Expand Up @@ -1127,7 +1136,7 @@ private String decodeReturnCode(int result) {
protected Toolchain getToolchain() throws MojoExecutionException {
if (JDKUsage.SYSTEM.equals(useJDK)) {
if (toolchainManager != null) {
return toolchainManager.getToolchainFromBuildContext("jdk", session);
return toolchainManager.getToolchainFromBuildContext(SYSTEM_JDK, session);
}
return null;
}
Expand Down Expand Up @@ -1209,16 +1218,44 @@ protected String getJavaExecutable() throws MojoExecutionException {
}
String javaHome = System.getenv("JAVA_HOME");
if (javaHome != null && !javaHome.isBlank()) {
File file = new File(javaHome, "bin/java");
if (file.exists()) {
getLog().info("Could not find the Toolchain, using java from JAVA_HOME instead");
return file.getAbsolutePath();
File java = getJavaFromJavaHome(javaHome);
if (java != null) {
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
+ ", using java from JAVA_HOME instead (" + java.getAbsolutePath() + ")");
return java.getAbsolutePath();
}
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
+ " and JAVA_HOME seem to not point to a valid location, trying java from PATH instead (current JAVA_HOME="
+ javaHome + ")");
} else {
getLog().info("Could not find a java toolchain of type " + SYSTEM_JDK
+ " and JAVA_HOME is not set, trying java from PATH instead");
}
getLog().info("Could not find the Toolchain nor JAVA_HOME, trying java from PATH instead");
return "java";
}

private File getJavaFromJavaHome(String javaHome) {
File javaBin = new File(javaHome, "bin");
for (String executable : JAVA_EXECUTABLES) {
File java = new File(javaBin, executable);
if (java.isFile()) {
return java;
}
}
//last resort just in case other extension or case-sensitive file-system...
File[] listFiles = javaBin.listFiles(new FileFilter() {

@Override
public boolean accept(File pathname) {
return pathname.isFile() && FilenameUtils.getBaseName(pathname.getName().toLowerCase()).equals("java");
}
});
if (listFiles != null && listFiles.length > 0) {
return listFiles[0];
}
return null;
}

private Map<String, String> getMergedSystemProperties() {
Map<String, String> result = new LinkedHashMap<>();
// bug 415489: use osgi.clean=true by default
Expand Down

0 comments on commit 4110030

Please sign in to comment.