From 624ecca3f641601c8dcd8a2ccbcded3db0a7d3bf Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Tue, 23 Jan 2024 16:05:04 +0100 Subject: [PATCH] [GERONIMO-6858] align defaults to graalvm 21 release --- .../ArthurNativeImageConfiguration.java | 39 +++++++++--- .../arthur/maven/mojo/NativeImageMojo.java | 60 ++++++++++++------- 2 files changed, 71 insertions(+), 28 deletions(-) diff --git a/arthur-impl/src/main/java/org/apache/geronimo/arthur/impl/nativeimage/ArthurNativeImageConfiguration.java b/arthur-impl/src/main/java/org/apache/geronimo/arthur/impl/nativeimage/ArthurNativeImageConfiguration.java index f381361..e6f1e74 100644 --- a/arthur-impl/src/main/java/org/apache/geronimo/arthur/impl/nativeimage/ArthurNativeImageConfiguration.java +++ b/arthur-impl/src/main/java/org/apache/geronimo/arthur/impl/nativeimage/ArthurNativeImageConfiguration.java @@ -16,15 +16,15 @@ */ package org.apache.geronimo.arthur.impl.nativeimage; -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; +import lombok.Data; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.util.ArrayList; import java.util.Collection; -import lombok.Data; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; @Data public class ArthurNativeImageConfiguration { @@ -76,12 +76,14 @@ public class ArthurNativeImageConfiguration { @GraalCommandPart(order = 16, template = "--static") private boolean buildStaticImage = true; + @Deprecated @GraalCommandPart(order = 17, template = "--allow-incomplete-classpath") private Boolean allowIncompleteClasspath; // recent version make it a default @GraalCommandPart(order = 18, template = "--report-unsupported-elements-at-runtime") private boolean reportUnsupportedElementsAtRuntime = true; + @Deprecated @GraalCommandPart(order = 19, template = "--enable-all-security-services") private Boolean enableAllSecurityServices = null; @@ -97,17 +99,40 @@ public class ArthurNativeImageConfiguration { private boolean inheritIO = true; /** - * @param graalVersion the graalvm version used to complete this configuration, in particular allowIncompleteClasspath and enableAllSecurityServices. + * @param graalVersion the graalvm version used to complete this configuration, in particular allowIncompleteClasspath and enableAllSecurityServices. + * @param hasEmbeddedResources are some META-INF/native-image/ files present. */ - public void complete(final String graalVersion) { + public void complete(final String graalVersion, final boolean hasEmbeddedResources) { if (graalVersion != null && (graalVersion.startsWith("21.") || graalVersion.startsWith("20."))) { if (allowIncompleteClasspath == null) { - allowIncompleteClasspath = true; + allowIncompleteClasspath = !graalVersion.contains("-graal"); // 21*graal* needs it false, previous 21 needs it true :facepalm: } if (enableAllSecurityServices == null) { - enableAllSecurityServices = true; + enableAllSecurityServices = !graalVersion.contains("-graal"); // 21*graal* needs it false, previous 21 needs it true :facepalm: } + } else { + // /!\ we disable this flag since recent graalvm versions don't need it anymore + allowIncompleteClasspath = false; + enableAllSecurityServices = false; } + if (!Boolean.getBoolean("arthur.unlockexperimentalvmoptions.skip") && + (customOptions == null || + (!customOptions.contains("-H:+UnlockExperimentalVMOptions") && !customOptions.contains("-H:-UnlockExperimentalVMOptions"))) && + hasEmbeddedResources) { + if (customOptions == null) { + customOptions = new ArrayList<>(); + } + customOptions.add("-H:+UnlockExperimentalVMOptions"); + } + } + + /** + * @param graalVersion the graalvm version used to complete this configuration, in particular allowIncompleteClasspath and enableAllSecurityServices. + * @deprecated use {@link #complete(String, boolean)}. + */ + @Deprecated + public void complete(final String graalVersion) { + complete(graalVersion, false); } public enum FallbackMode { diff --git a/arthur-maven-plugin/src/main/java/org/apache/geronimo/arthur/maven/mojo/NativeImageMojo.java b/arthur-maven-plugin/src/main/java/org/apache/geronimo/arthur/maven/mojo/NativeImageMojo.java index 81b3c42..1e62000 100644 --- a/arthur-maven-plugin/src/main/java/org/apache/geronimo/arthur/maven/mojo/NativeImageMojo.java +++ b/arthur-maven-plugin/src/main/java/org/apache/geronimo/arthur/maven/mojo/NativeImageMojo.java @@ -73,8 +73,10 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.function.Predicate; +import java.util.jar.JarFile; import java.util.stream.Stream; import java.util.stream.StreamSupport; +import java.util.zip.ZipEntry; import static java.lang.ClassLoader.getSystemClassLoader; import static java.util.Comparator.comparing; @@ -190,6 +192,7 @@ public class NativeImageMojo extends ArthurMojo { /** * Should incomplete classpath be tolerated. */ + @Deprecated @Parameter(property = "arthur.allowIncompleteClasspath") private Boolean allowIncompleteClasspath; @@ -202,6 +205,7 @@ public class NativeImageMojo extends ArthurMojo { /** * Should security services be included. */ + @Deprecated @Parameter(property = "arthur.enableAllSecurityServices") private Boolean enableAllSecurityServices; @@ -390,7 +394,21 @@ public void execute() { final Map classpathEntries = findClasspathFiles().collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a)); final ArthurNativeImageConfiguration configuration = getConfiguration(classpathEntries.values()); - configuration.complete(graalVersion); + configuration.complete(graalVersion, classpathEntries.values().stream() + .anyMatch(p -> { + if (Files.isDirectory(p)) { + return Files.exists(p.resolve("META-INF/native-image")); + } + if (Files.exists(p)) { + try (final JarFile jar = new JarFile(p.toFile())) { + final ZipEntry entry = jar.getEntry("META-INF/native-image"); + return entry != null && entry.isDirectory(); + } catch (final IOException e) { + return false; + } + } + return false; + })); if (nativeImage == null) { final SdkmanGraalVMInstaller graalInstaller = createInstaller(); @@ -481,10 +499,10 @@ protected Class loadClass(final String name, final boolean resolve) throws Cl @Override protected Iterable loadExtensions() { return Stream.concat( - // classloading bypasses them since TCCL is a fake loader with the JVM as parent - Stream.of(new AnnotationExtension(), new MavenArthurExtension()), - // graalextensions - StreamSupport.stream(super.loadExtensions().spliterator(), false)) + // classloading bypasses them since TCCL is a fake loader with the JVM as parent + Stream.of(new AnnotationExtension(), new MavenArthurExtension()), + // graalextensions + StreamSupport.stream(super.loadExtensions().spliterator(), false)) // ensure we dont duplicate any extension .distinct() .sorted(comparing(ArthurExtension::order)) @@ -547,22 +565,22 @@ private Stream> findClasspathFiles final Artifact artifactGav = new org.apache.maven.artifact.DefaultArtifact( groupId, artifactId, version, "compile", packaging, null, new DefaultArtifactHandler()); return Stream.concat(Stream.concat( - usePackagedArtifact ? - Stream.of(jar).map(j -> new AbstractMap.SimpleImmutableEntry<>(artifactGav, j.toPath())) : - Stream.concat( - Stream.of(classes).map(j -> new AbstractMap.SimpleImmutableEntry<>(artifactGav, j.toPath())), - supportTestArtifacts ? Stream.of(testClasses).>map(j -> - new AbstractMap.SimpleImmutableEntry<>(new org.apache.maven.artifact.DefaultArtifact( - groupId, artifactId, version, "compile", packaging, "test", new DefaultArtifactHandler()), - j.toPath())) : - Stream.empty()), - project.getArtifacts().stream() - .filter(a -> !excludedArtifacts.contains(a.getGroupId() + ':' + a.getArtifactId())) - .filter(this::handleTestInclusion) - .filter(this::isNotSvm) - .filter(a -> supportedTypes.contains(a.getType())) - .map(a -> new AbstractMap.SimpleImmutableEntry<>(a, a.getFile().toPath()))), - resolveExtension()) + usePackagedArtifact ? + Stream.of(jar).map(j -> new AbstractMap.SimpleImmutableEntry<>(artifactGav, j.toPath())) : + Stream.concat( + Stream.of(classes).map(j -> new AbstractMap.SimpleImmutableEntry<>(artifactGav, j.toPath())), + supportTestArtifacts ? Stream.of(testClasses).>map(j -> + new AbstractMap.SimpleImmutableEntry<>(new org.apache.maven.artifact.DefaultArtifact( + groupId, artifactId, version, "compile", packaging, "test", new DefaultArtifactHandler()), + j.toPath())) : + Stream.empty()), + project.getArtifacts().stream() + .filter(a -> !excludedArtifacts.contains(a.getGroupId() + ':' + a.getArtifactId())) + .filter(this::handleTestInclusion) + .filter(this::isNotSvm) + .filter(a -> supportedTypes.contains(a.getType())) + .map(a -> new AbstractMap.SimpleImmutableEntry<>(a, a.getFile().toPath()))), + resolveExtension()) .filter(e -> Files.exists(e.getValue())); }