Skip to content

Commit

Permalink
Fix eclipse-tycho#849 JAVA_HOME check is not OS independent
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Apr 6, 2022
1 parent f69b9e1 commit ced6c2d
Showing 1 changed file with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* 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
******************************************************************************/
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 @@ -35,6 +37,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 @@ -103,6 +106,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 @@ -111,6 +116,8 @@ public abstract class AbstractTestMojo extends AbstractMojo {

private static final ConcurrencyLock CONCURRENCY_LOCK = new ConcurrencyLock();

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 @@ -1152,7 +1159,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 @@ -1234,16 +1241,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

1 comment on commit ced6c2d

@fipro78
Copy link

@fipro78 fipro78 commented on ced6c2d Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Please sign in to comment.