15
15
* Christoph Läubrich - [Bug 529929] improve error message in case of failures
16
16
* - [Bug 572420] Tycho-Surefire should be executable for eclipse-plugin package type
17
17
* - [Issue 790] Support printing of bundle wirings in tycho-surefire-plugin
18
+ * - [Issue 849] JAVA_HOME check is not OS independent
18
19
******************************************************************************/
19
20
package org .eclipse .tycho .surefire ;
20
21
21
22
import java .io .BufferedOutputStream ;
22
23
import java .io .File ;
24
+ import java .io .FileFilter ;
23
25
import java .io .FileOutputStream ;
24
26
import java .io .IOException ;
25
27
import java .net .MalformedURLException ;
35
37
import java .util .Set ;
36
38
import java .util .StringJoiner ;
37
39
40
+ import org .apache .commons .io .FilenameUtils ;
38
41
import org .apache .maven .artifact .Artifact ;
39
42
import org .apache .maven .artifact .resolver .ArtifactResolutionException ;
40
43
import org .apache .maven .artifact .resolver .ArtifactResolutionRequest ;
103
106
104
107
public abstract class AbstractTestMojo extends AbstractMojo {
105
108
109
+ private static final String SYSTEM_JDK = "jdk" ;
110
+
106
111
private static String [] UNIX_SIGNAL_NAMES = { "not a signal" , // padding, signals start with 1
107
112
"SIGHUP" , "SIGINT" , "SIGQUIT" , "SIGILL" , "SIGTRAP" , "SIGABRT" , "SIGBUS" , "SIGFPE" , "SIGKILL" , "SIGUSR1" ,
108
113
"SIGSEGV" , "SIGUSR2" , "SIGPIPE" , "SIGALRM" , "SIGTERM" , "SIGSTKFLT" , "SIGCHLD" , "SIGCONT" , "SIGSTOP" ,
@@ -111,6 +116,8 @@ public abstract class AbstractTestMojo extends AbstractMojo {
111
116
112
117
private static final ConcurrencyLock CONCURRENCY_LOCK = new ConcurrencyLock ();
113
118
119
+ private static final String [] JAVA_EXECUTABLES = { "java" , "java.exe" };
120
+
114
121
/**
115
122
* Root directory (<a href=
116
123
* "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) {
1152
1159
protected Toolchain getToolchain () throws MojoExecutionException {
1153
1160
if (JDKUsage .SYSTEM .equals (useJDK )) {
1154
1161
if (toolchainManager != null ) {
1155
- return toolchainManager .getToolchainFromBuildContext ("jdk" , session );
1162
+ return toolchainManager .getToolchainFromBuildContext (SYSTEM_JDK , session );
1156
1163
}
1157
1164
return null ;
1158
1165
}
@@ -1234,16 +1241,44 @@ protected String getJavaExecutable() throws MojoExecutionException {
1234
1241
}
1235
1242
String javaHome = System .getenv ("JAVA_HOME" );
1236
1243
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 ();
1241
1249
}
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" );
1242
1256
}
1243
- getLog ().info ("Could not find the Toolchain nor JAVA_HOME, trying java from PATH instead" );
1244
1257
return "java" ;
1245
1258
}
1246
1259
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
+
1247
1282
private Map <String , String > getMergedSystemProperties () {
1248
1283
Map <String , String > result = new LinkedHashMap <>();
1249
1284
// bug 415489: use osgi.clean=true by default
0 commit comments