From 81fe799e1e9c8bf874712211f22f42ba6341c649 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 30 Aug 2024 16:36:44 +0200 Subject: [PATCH] Fix error message when the JDK is not a GraalVM --- .../NativeImageExecutableLocator.java | 42 +++++++++---------- .../utils/NativeImageConfigurationUtils.java | 31 +++++++------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageExecutableLocator.java b/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageExecutableLocator.java index ae05fcb61..0a77f440d 100644 --- a/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageExecutableLocator.java +++ b/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageExecutableLocator.java @@ -91,29 +91,29 @@ public static File findNativeImageExecutable(Property javaLauncher executablePath = metadata.getInstallationPath().file("bin/" + NATIVE_IMAGE_EXE).getAsFile(); } - try { - if (!executablePath.exists()) { - logger.log("Native Image executable wasn't found. We will now try to download it. "); - File graalVmHomeGuess = executablePath.getParentFile(); - - File guPath = graalVmHomeGuess.toPath().resolve(GU_EXE).toFile(); - if (!guPath.exists()) { - throw new GradleException("'" + GU_EXE + "' at '" + guPath + "' tool wasn't found. This probably means that JDK at isn't a GraalVM distribution."); - } - ExecResult res = execOperations.exec(spec -> { - spec.args("install", "native-image"); - spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE)); - }); - if (res.getExitValue() != 0) { - throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it."); - } - diagnostics.withGuInstall(); + File graalVmHomeGuess = executablePath.getParentFile(); + File guPath = graalVmHomeGuess.toPath().resolve(GU_EXE).toFile(); + if (guPath.exists() && !executablePath.exists()) { + logger.log("Native Image executable wasn't found. We will now try to download it. "); + + ExecResult res = execOperations.exec(spec -> { + spec.args("install", "native-image"); + spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE)); + }); + if (res.getExitValue() != 0) { + throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it.\n" + + "Make sure to declare the GRAALVM_HOME or JAVA_HOME environment variable or install GraalVM with " + + "native-image in a standard location recognized by Gradle Java toolchain support"); } - } catch (GradleException e) { - throw new GradleException("Determining GraalVM installation failed with message: " + e.getMessage() + "\n\n" - + "Make sure to declare the GRAALVM_HOME environment variable or install GraalVM with " + - "native-image in a standard location recognized by Gradle Java toolchain support"); + diagnostics.withGuInstall(); } + + if (!executablePath.exists()) { + throw new GradleException(executablePath + " wasn't found. This probably means that JDK isn't a GraalVM distribution.\n" + + "Make sure to declare the GRAALVM_HOME or JAVA_HOME environment variable or install GraalVM with" + + "native-image in a standard location recognized by Gradle Java toolchain support"); + } + diagnostics.withExecutablePath(executablePath); return executablePath; } diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/NativeImageConfigurationUtils.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/NativeImageConfigurationUtils.java index e3401033b..364e051c6 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/NativeImageConfigurationUtils.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/NativeImageConfigurationUtils.java @@ -69,34 +69,31 @@ public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failF Path graalHomePath = Paths.get(graalHome); Path nativeImageExe = graalHomePath.resolve("bin").resolve(NATIVE_IMAGE_EXE); + Path guExe = graalHomePath.resolve("bin").resolve(GU_EXE); - if (!Files.exists(nativeImageExe)) { - Path guExe = graalHomePath.resolve("bin").resolve(GU_EXE); - if (Files.exists(guExe)) { - ProcessBuilder processBuilder = new ProcessBuilder(guExe.toString(), "install", "native-image"); - processBuilder.inheritIO(); - try { - Process nativeImageFetchingProcess = processBuilder.start(); - if (nativeImageFetchingProcess.waitFor() != 0) { - throw new MojoExecutionException("native-image was not found, and '" + GU_EXE + "' tool failed to install it."); - } - } catch (MojoExecutionException | IOException | InterruptedException e) { - throw new MojoExecutionException("Determining GraalVM installation failed with message: " + e.getMessage()); + if (Files.exists(guExe) && !Files.exists(nativeImageExe)) { + ProcessBuilder processBuilder = new ProcessBuilder(guExe.toString(), "install", "native-image"); + processBuilder.inheritIO(); + try { + Process nativeImageFetchingProcess = processBuilder.start(); + if (nativeImageFetchingProcess.waitFor() != 0) { + throw new MojoExecutionException("native-image was not found, and '" + GU_EXE + "' tool failed to install it."); } - } else if (failFast) { - throw new MojoExecutionException("'" + GU_EXE + "' tool was not found in your " + javaHomeVariable + "." + - "This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution."); + } catch (MojoExecutionException | IOException | InterruptedException e) { + throw new MojoExecutionException("Determining GraalVM installation failed with message: " + e.getMessage()); } } if (!Files.exists(nativeImageExe)) { if (failFast) { - throw new RuntimeException("native-image is not installed in your " + javaHomeVariable + "." + - "This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution."); + throw new MojoExecutionException("native-image is not installed in your " + javaHomeVariable + "." + + "This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution. " + + "The GraalVM Native Maven Plugin requires GRAALVM_HOME or JAVA_HOME to be a GraalVM distribution."); } else { return null; } } + logger.info("Found GraalVM installation from " + javaHomeVariable + " variable."); return nativeImageExe; }