|
61 | 61 | import org.gradle.jvm.toolchain.JavaInstallationMetadata; |
62 | 62 | import org.gradle.language.base.plugins.LifecycleBasePlugin; |
63 | 63 | import org.gradle.process.ExecOperations; |
| 64 | +import org.gradle.process.ExecResult; |
64 | 65 |
|
65 | 66 | import javax.inject.Inject; |
66 | 67 | import java.io.File; |
67 | 68 | import java.nio.file.Paths; |
68 | 69 | import java.util.List; |
69 | 70 |
|
| 71 | +import static org.graalvm.buildtools.utils.SharedConstants.GU_EXE; |
70 | 72 | import static org.graalvm.buildtools.utils.SharedConstants.NATIVE_IMAGE_EXE; |
71 | 73 |
|
72 | 74 | /** |
@@ -154,22 +156,40 @@ public void exec() { |
154 | 156 | if (!executablePath.exists() && getGraalVMHome().isPresent()) { |
155 | 157 | executablePath = Paths.get(getGraalVMHome().get()).resolve("bin").resolve(NATIVE_IMAGE_EXE).toFile(); |
156 | 158 | } |
157 | | - if (executablePath.exists()) { |
158 | | - logger.log("Using executable path: " + executablePath); |
159 | | - String executable = executablePath.getAbsolutePath(); |
160 | | - File outputDir = getOutputDirectory().getAsFile().get(); |
161 | | - if (outputDir.isDirectory() || outputDir.mkdirs()) { |
162 | | - getExecOperations().exec(spec -> { |
163 | | - spec.setWorkingDir(getWorkingDirectory()); |
164 | | - spec.args(args); |
165 | | - getService().get(); |
166 | | - spec.setExecutable(executable); |
| 159 | + |
| 160 | + try { |
| 161 | + if (!executablePath.exists()) { |
| 162 | + logger.log("Native Image executable wasn't found. We will now try to download it. "); |
| 163 | + File graalVmHomeGuess = executablePath.getParentFile(); |
| 164 | + |
| 165 | + if (!graalVmHomeGuess.toPath().resolve(GU_EXE).toFile().exists()) { |
| 166 | + throw new GradleException("'" + GU_EXE + "' tool wasn't found. This probably means that JDK at isn't a GraalVM distribution."); |
| 167 | + } |
| 168 | + ExecResult res = getExecOperations().exec(spec -> { |
| 169 | + spec.args("install", "native-image"); |
| 170 | + spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE)); |
167 | 171 | }); |
168 | | - logger.lifecycle("Native Image written to: " + outputDir); |
| 172 | + if (res.getExitValue() != 0) { |
| 173 | + throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it."); |
| 174 | + } |
169 | 175 | } |
170 | | - return; |
| 176 | + } catch (GradleException e) { |
| 177 | + throw new GradleException("Determining GraalVM installation failed with message: " + e.getMessage() + "\n\n" |
| 178 | + + "Make sure to declare the GRAALVM_HOME environment variable or install GraalVM with " + |
| 179 | + "native-image in a standard location recognized by Gradle Java toolchain support"); |
| 180 | + } |
| 181 | + |
| 182 | + logger.log("Using executable path: " + executablePath); |
| 183 | + String executable = executablePath.getAbsolutePath(); |
| 184 | + File outputDir = getOutputDirectory().getAsFile().get(); |
| 185 | + if (outputDir.isDirectory() || outputDir.mkdirs()) { |
| 186 | + getExecOperations().exec(spec -> { |
| 187 | + spec.setWorkingDir(getWorkingDirectory()); |
| 188 | + spec.args(args); |
| 189 | + getService().get(); |
| 190 | + spec.setExecutable(executable); |
| 191 | + }); |
| 192 | + logger.lifecycle("Native Image written to: " + outputDir); |
171 | 193 | } |
172 | | - throw new GradleException("Expected to find " + NATIVE_IMAGE_EXE + " executable but it wasn't found. " + |
173 | | - "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"); |
174 | 194 | } |
175 | 195 | } |
0 commit comments