From f3baa7c3b8296a94255b4d6b4574e81a764f430f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serge=20D=C3=A9moulin?= Date: Wed, 11 Sep 2024 19:27:26 +0200 Subject: [PATCH] supporting jdk toolchain configuration --- .../maven/AbstractNativeImageMojo.java | 9 +++- .../config/AbstractMergeAgentFilesMojo.java | 18 ++++++- .../utils/NativeImageConfigurationUtils.java | 49 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java index 35a09382b..cdb291f73 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/AbstractNativeImageMojo.java @@ -171,6 +171,13 @@ public abstract class AbstractNativeImageMojo extends AbstractNativeMojo { @Component protected ToolchainManager toolchainManager; + /** + Set this to true to ensure that native-image is found in your toolchain and not in an unrelated JDK or in your PATH. + Will fail the build in case no toolchain was found or if it does not contain native-image. + */ + @Parameter(property = "enforceToolchain") + protected boolean enforceToolchain; + @Inject protected AbstractNativeImageMojo() { imageClasspath = new ArrayList<>(); @@ -411,7 +418,7 @@ protected String getClasspath() throws MojoExecutionException { protected void buildImage() throws MojoExecutionException { checkRequiredVersionIfNeeded(); - Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger); + Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImageSupportingToolchain(logger, toolchainManager, session, enforceToolchain); try { ProcessBuilder processBuilder = new ProcessBuilder(nativeImageExecutable.toString()); diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/config/AbstractMergeAgentFilesMojo.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/config/AbstractMergeAgentFilesMojo.java index 3323845ea..6b3ec2292 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/config/AbstractMergeAgentFilesMojo.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/config/AbstractMergeAgentFilesMojo.java @@ -41,9 +41,12 @@ package org.graalvm.buildtools.maven.config; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.toolchain.ToolchainManager; import org.codehaus.plexus.logging.Logger; import org.graalvm.buildtools.utils.NativeImageConfigurationUtils; @@ -58,6 +61,19 @@ public abstract class AbstractMergeAgentFilesMojo extends AbstractMojo { @Component protected Logger logger; + @Parameter(defaultValue = "${session}", readonly = true) + protected MavenSession session; + + @Component + protected ToolchainManager toolchainManager; + + /** + Set this to true to ensure that native-image is found in your toolchain and not in an unrelated JDK or in your PATH. + Will fail the build in case no toolchain was found or if it does not contain native-image. + */ + @Parameter(property = "enforceToolchain") + protected boolean enforceToolchain; + private File mergerExecutable; public File getMergerExecutable() throws MojoExecutionException { @@ -69,7 +85,7 @@ public File getMergerExecutable() throws MojoExecutionException { } private void initializeMergerExecutable() throws MojoExecutionException { - Path nativeImage = NativeImageConfigurationUtils.getNativeImage(logger); + Path nativeImage = NativeImageConfigurationUtils.getNativeImageSupportingToolchain(logger, toolchainManager, session, enforceToolchain); File nativeImageExecutable = nativeImage.toAbsolutePath().toFile(); String nativeImageConfigureFileName = nativeImageConfigureFileName(); File mergerExecutable = new File(nativeImageExecutable.getParentFile(), nativeImageConfigureFileName); 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 364e051c6..b83902847 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 @@ -41,7 +41,10 @@ package org.graalvm.buildtools.utils; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.toolchain.Toolchain; +import org.apache.maven.toolchain.ToolchainManager; import org.codehaus.plexus.logging.Logger; import java.io.File; @@ -60,6 +63,7 @@ public abstract class NativeImageConfigurationUtils implements SharedConstants { public static final String NATIVE_TESTS_EXE = "native-tests" + EXECUTABLE_EXTENSION; public static final String MAVEN_GROUP_ID = "org.graalvm.buildtools"; public static Path nativeImageExeCache; + public static Path nativeImageExeCacheSupportingToolchain; public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failFast, Logger logger) throws MojoExecutionException { String graalHome = System.getenv(javaHomeVariable); @@ -106,6 +110,51 @@ public static Path getNativeImageFromPath() { return exePath.map(path -> path.resolve(NATIVE_IMAGE_EXE)).orElse(null); } + public static Path getNativeImageSupportingToolchain(Logger logger, ToolchainManager toolchainManager, MavenSession session, boolean enforceToolchain) throws MojoExecutionException { + if (nativeImageExeCacheSupportingToolchain != null) { + return nativeImageExeCacheSupportingToolchain; + } + + Path nativeImage = getToolchainNativeImage(logger, toolchainManager, session, enforceToolchain); + if (nativeImage != null) { + nativeImageExeCacheSupportingToolchain = nativeImage; + nativeImageExeCache = nativeImage; + return nativeImage; + } + + return getNativeImage(logger); + } + + public static Path getToolchainNativeImage(Logger logger, ToolchainManager toolchainManager, MavenSession session, boolean enforceToolchain) throws MojoExecutionException { + final Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", session); + + if (toolchain != null) { + String javaPath = toolchain.findTool("java"); + + if (javaPath != null) { + Path nativeImagePath = Paths.get(javaPath).getParent().resolve(NATIVE_IMAGE_EXE).toAbsolutePath(); + if (!Files.exists(nativeImagePath)) { + final String message = "No " + NATIVE_IMAGE_EXE + " found in the jdk toolchain configuration: " + nativeImagePath.getParent().getParent(); + if (enforceToolchain) { + throw new MojoExecutionException(message); + } + logger.warn(message); + return null; + } + return nativeImagePath; + } + throw new MojoExecutionException("No java found the toolchain configuration."); + + } else { + final String message = "No jdk toolchain configuration found"; + if (enforceToolchain) { + throw new MojoExecutionException(message); + } + logger.warn(message); + } + return null; + } + public static Path getNativeImage(Logger logger) throws MojoExecutionException { if (nativeImageExeCache != null) { return nativeImageExeCache;