From f36724b75ffcedc4c8c7cdb05a2c449c2cd58080 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 25 Aug 2021 13:17:53 +0300 Subject: [PATCH 1/2] Add the ability for extensions to exclude GraalVM config from jars Resolves: #17482 --- .../nativeimage/ExcludeConfigBuildItem.java | 41 +++++++++++++++++++ .../pkg/steps/NativeImageBuildStep.java | 20 ++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java diff --git a/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java b/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java new file mode 100644 index 0000000000000..fc6e88f8116f8 --- /dev/null +++ b/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java @@ -0,0 +1,41 @@ +package io.quarkus.deployment.builditem.nativeimage; + +import io.quarkus.builder.item.MultiBuildItem; + +/** + * A build item that allows extension to configure the native-image compiler to effectively + * ignore certain configuration files in specific jars. + * + * The {@code jarFile} property specifies the name of the jar file or a regular expression that can be used to + * match multiple jar files. + * Matching jar files using regular expressions should be done as a last resort. + * + * The {@code resourceName} property specifies the name of the resource file or a regular expression that can be used to + * match multiple resource files. + * For the match to work, the resources need to be part of the matched jar file(s) (see {@code jarFile}). + * Matching resource files using regular expressions should be done as a last resort. + * + * See https://github.com/oracle/graal/pull/3179 for more details. + */ +public final class ExcludeConfigBuildItem extends MultiBuildItem { + + private final String jarFile; + private final String resourceName; + + public ExcludeConfigBuildItem(String jarFile, String resourceName) { + this.jarFile = jarFile; + this.resourceName = resourceName; + } + + public ExcludeConfigBuildItem(String jarFile) { + this(jarFile, "META-INF/native-image/native-image.properties"); + } + + public String getJarFile() { + return jarFile; + } + + public String getResourceName() { + return resourceName; + } +} diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java index 2210a067887cb..e1dba7ed6f7e9 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java @@ -26,6 +26,7 @@ import io.quarkus.bootstrap.model.AppDependency; import io.quarkus.bootstrap.util.IoUtils; import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.builditem.nativeimage.ExcludeConfigBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageSecurityProviderBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem; import io.quarkus.deployment.pkg.NativeConfig; @@ -81,7 +82,8 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig, NativeImageSourceJarBuildItem nativeImageSourceJarBuildItem, OutputTargetBuildItem outputTargetBuildItem, PackageConfig packageConfig, - List nativeImageProperties) { + List nativeImageProperties, + List excludeConfigs) { Path outputDir; try { @@ -100,6 +102,7 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig, .setNativeConfig(nativeConfig) .setOutputTargetBuildItem(outputTargetBuildItem) .setNativeImageProperties(nativeImageProperties) + .setExcludeConfigs(excludeConfigs) .setOutputDir(outputDir) .setRunnerJarName(runnerJar.getFileName().toString()) // the path to native-image is not known now, it is only known at the time the native-sources will be consumed @@ -130,6 +133,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa PackageConfig packageConfig, CurateOutcomeBuildItem curateOutcomeBuildItem, List nativeImageProperties, + List excludeConfigs, List nativeImageSecurityProviders, Optional processInheritIODisabled) { if (nativeConfig.debug.enabled) { @@ -187,6 +191,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa .setNativeConfig(nativeConfig) .setOutputTargetBuildItem(outputTargetBuildItem) .setNativeImageProperties(nativeImageProperties) + .setExcludeConfigs(excludeConfigs) .setNativeImageSecurityProviders(nativeImageSecurityProviders) .setOutputDir(outputDir) .setRunnerJarName(runnerJarName) @@ -477,6 +482,7 @@ static class Builder { private NativeConfig nativeConfig; private OutputTargetBuildItem outputTargetBuildItem; private List nativeImageProperties; + private List excludeConfigs; private List nativeImageSecurityProviders; private Path outputDir; private String runnerJarName; @@ -500,6 +506,11 @@ public Builder setNativeImageProperties(List return this; } + public Builder setExcludeConfigs(List excludeConfigs) { + this.excludeConfigs = excludeConfigs; + return this; + } + public Builder setNativeImageSecurityProviders( List nativeImageSecurityProviders) { this.nativeImageSecurityProviders = nativeImageSecurityProviders; @@ -700,6 +711,13 @@ public NativeImageInvokerInfo build() { .collect(Collectors.joining(",")); nativeImageArgs.add("-H:AdditionalSecurityProviders=" + additionalSecurityProviders); } + + // --exclude-config options + for (ExcludeConfigBuildItem excludeConfig : excludeConfigs) { + nativeImageArgs.add("--exclude-config"); + nativeImageArgs.add(excludeConfig.getJarFile()); + nativeImageArgs.add(excludeConfig.getResourceName()); + } } nativeImageArgs.add(nativeImageName); From 58e48e0594f43992a89209826da98348f41ac84e Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 31 Aug 2021 17:23:08 +0100 Subject: [PATCH 2/2] Make sure the --exclude-config arguments precede the -jar arguments --- .../builditem/nativeimage/ExcludeConfigBuildItem.java | 2 +- .../quarkus/deployment/pkg/steps/NativeImageBuildStep.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java b/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java index fc6e88f8116f8..8c2cf955c2639 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ExcludeConfigBuildItem.java @@ -28,7 +28,7 @@ public ExcludeConfigBuildItem(String jarFile, String resourceName) { } public ExcludeConfigBuildItem(String jarFile) { - this(jarFile, "META-INF/native-image/native-image.properties"); + this(jarFile, "/META-INF/native-image/native-image\\.properties"); } public String getJarFile() { diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java index e1dba7ed6f7e9..2236dd642d26b 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java @@ -595,8 +595,6 @@ public NativeImageInvokerInfo build() { "-H:InitialCollectionPolicy=com.oracle.svm.core.genscavenge.CollectionPolicy$BySpaceAndTime"); //the default collection policy results in full GC's 50% of the time nativeImageArgs.add("-H:+JNI"); nativeImageArgs.add("-H:+AllowFoldMethods"); - nativeImageArgs.add("-jar"); - nativeImageArgs.add(runnerJarName); if (nativeConfig.enableFallbackImages) { nativeImageArgs.add("-H:FallbackThreshold=5"); @@ -722,6 +720,10 @@ public NativeImageInvokerInfo build() { nativeImageArgs.add(nativeImageName); + //Make sure to have the -jar as last one, as it otherwise breaks "--exclude-config" + nativeImageArgs.add("-jar"); + nativeImageArgs.add(runnerJarName); + return new NativeImageInvokerInfo(nativeImageArgs); }