Skip to content

Commit

Permalink
Add exclude-config option oracle#2535
Browse files Browse the repository at this point in the history
  • Loading branch information
galderz committed Jan 27, 2021
1 parent 8826307 commit 96c0c0d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.Queue;
import java.util.regex.Pattern;

import org.graalvm.compiler.options.OptionType;

Expand Down Expand Up @@ -151,6 +152,18 @@ public boolean consume(Queue<String> args) {
args.poll();
NativeImage.showWarning("Ignoring server-mode native-image argument " + headArg + ".");
return true;
case "--exclude-config":
args.poll();
String excludeConfig = args.poll();
if (excludeConfig == null) {
NativeImage.showError(headArg + " requires a jar and resource regular expression, comma separated");
}
final String[] split = excludeConfig.split(",");
if (split.length < 2) {
NativeImage.showError(headArg + " value '" + excludeConfig + " not correctly formatted. It requires a jar and resource regular expression, comma separated");
}
nativeImage.addExcludeConfig(Paths.get(split[0]), Pattern.compile(split[1]));
return true;
}

String debugAttach = "--debug-attach";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -226,6 +227,8 @@ private static <T> String oR(OptionKey<T> option) {
final Registry optionRegistry;
private LinkedHashSet<EnabledOption> enabledLanguages;

private final Map<Path, Pattern> excludedConfigs = new HashMap<>();

public static final String nativeImagePropertiesFilename = "native-image.properties";
public static final String nativeImageMetaInf = "META-INF/native-image";

Expand Down Expand Up @@ -934,6 +937,10 @@ 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<String, String> resolver = str -> {
Path componentDirectory = resourceRoot.relativize(nativeImageMetaInfFile).getParent();
Expand All @@ -956,6 +963,16 @@ private void processNativeImageMetaInf(Path classpathEntry, Path nativeImageMeta
}
}

public void addExcludeConfig(Path jarPath, Pattern resourceRegex) {
excludedConfigs.put(jarPath, resourceRegex);
}

private boolean isExcluded(Path resourcePath, Path classpathEntry) {
return excludedConfigs.entrySet().stream()
.filter(e -> classpathEntry.endsWith(e.getKey()))
.anyMatch(e -> e.getValue().matcher(resourcePath.toString()).find());
}

private void processNativeImageMetaInf(@SuppressWarnings("unused") Path classpathEntry, Path resourceRoot,
Path resourcePath, MetaInfFileType resourceType, Function<String, String> resolver) throws IOException {

Expand Down

0 comments on commit 96c0c0d

Please sign in to comment.