diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java index f6e4f9cb454f..9ad9c2fc7bae 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/DefaultOptionHandler.java @@ -29,6 +29,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.regex.Pattern; import org.graalvm.compiler.options.OptionType; @@ -151,6 +152,18 @@ public boolean consume(ArgumentQueue args) { args.poll(); NativeImage.showWarning("Ignoring server-mode native-image argument " + headArg + "."); return true; + case "--exclude-config": + args.poll(); + String excludeJar = args.poll(); + if (excludeJar == null) { + NativeImage.showError(headArg + " requires two arguments: a jar regular expression and a resource regular expression"); + } + String excludeConfig = args.poll(); + if (excludeConfig == null) { + NativeImage.showError(headArg + " requires resource regular expression"); + } + nativeImage.addExcludeConfig(Pattern.compile(excludeJar), Pattern.compile(excludeConfig)); + return true; } String debugAttach = "--debug-attach"; diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index 54c00951aaa1..0fc80ee447cf 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -263,6 +263,8 @@ private static String oR(OptionKey option) { final Registry optionRegistry; private LinkedHashSet enabledLanguages; + private final List excludedConfigs = new ArrayList<>(); + public static final String nativeImagePropertiesFilename = "native-image.properties"; public static final String nativeImageMetaInf = "META-INF/native-image"; @@ -978,6 +980,11 @@ private void processNativeImageMetaInf(Path classpathEntry, Path nativeImageMeta .filter(p -> p.endsWith(fileType.fileName)) .collect(Collectors.toList()); for (Path nativeImageMetaInfFile : nativeImageMetaInfFiles) { + boolean excluded = isExcluded(nativeImageMetaInfFile, classpathEntry); + if (excluded) { + continue; + } + Path resourceRoot = nativeImageMetaInfBase.getParent().getParent(); Function resolver = str -> { Path componentDirectory = resourceRoot.relativize(nativeImageMetaInfFile).getParent(); @@ -1000,6 +1007,16 @@ private void processNativeImageMetaInf(Path classpathEntry, Path nativeImageMeta } } + public void addExcludeConfig(Pattern jarPattern, Pattern resourcePattern) { + excludedConfigs.add(new ExcludeConfig(jarPattern, resourcePattern)); + } + + private boolean isExcluded(Path resourcePath, Path classpathEntry) { + return excludedConfigs.stream() + .filter(e -> e.jarPattern.matcher(classpathEntry.toString()).find()) + .anyMatch(e -> e.resourcePattern.matcher(resourcePath.toString()).find()); + } + static String injectHostedOptionOrigin(String option, String origin) { if (origin != null && option.startsWith(oH)) { String optionOriginSeparator = "@"; @@ -1925,4 +1942,14 @@ public static void main(String[] args) { NativeImage.main(args); } } + + private static final class ExcludeConfig { + final Pattern jarPattern; + final Pattern resourcePattern; + + private ExcludeConfig(Pattern jarPattern, Pattern resourcePattern) { + this.jarPattern = jarPattern; + this.resourcePattern = resourcePattern; + } + } }