Skip to content

Commit

Permalink
Process custom image classpath after parsing options oracle#3742
Browse files Browse the repository at this point in the history
* Custom image classpath processing can be impacted by exclude-config.
* Moved the code out of -jar parsing,
to avoid the need for specific parameter order.
* Without this change,
--exclude-config needs to be passed before -jar
for it to have an effect.
* Added verbose messages for easier debugging of native-image binary.
  • Loading branch information
galderz committed Sep 2, 2021
1 parent 43eb09e commit 682856d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public boolean consume(ArgumentQueue args) {
if (jarFilePathStr == null) {
NativeImage.showError(requireValidJarFileMessage);
}
handleJarFileArg(nativeImage.canonicalize(Paths.get(jarFilePathStr)));
final Path filePath = nativeImage.canonicalize(Paths.get(jarFilePathStr));
nativeImage.setJarFilePath(filePath);
handleJarFileArg(filePath);
nativeImage.setJarOptionMode(true);
return true;
case verboseOption:
Expand Down Expand Up @@ -319,7 +321,6 @@ private void handleJarFileArg(Path filePath) {
if (!NativeImage.processJarManifestMainAttributes(filePath, nativeImage::handleMainClassAttribute)) {
NativeImage.showError("No manifest in " + filePath);
}
nativeImage.addCustomImageClasspath(filePath);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ private static <T> String oR(OptionKey<T> option) {
private boolean diagnostics = false;
String diagnosticsDir;
private boolean jarOptionMode = false;
private Path jarFilePath;
private boolean dryRun = false;
private String printFlagsOptionQuery = null;
private String printFlagsWithExtraHelpOptionQuery = null;
Expand Down Expand Up @@ -688,9 +689,18 @@ public void showVerboseMessage(String message) {

@Override
public boolean isExcluded(Path resourcePath, Path classpathEntry) {
showVerboseMessage(String.format("Attempt to check if resource %s in classpath entry %s should be excluded", resourcePath, classpathEntry));
return excludedConfigs.stream()
.filter(e -> e.jarPattern.matcher(classpathEntry.toString()).find())
.anyMatch(e -> e.resourcePattern.matcher(resourcePath.toString()).find());
.filter(e -> regexMatch(classpathEntry, e.jarPattern, "Classpath entry"))
.anyMatch(e -> regexMatch(resourcePath, e.resourcePattern, "Resource path"));
}

private boolean regexMatch(Path path, Pattern pattern, String prefix) {
final boolean matches = pattern.matcher(path.toString()).find();
if (verbose) {
showVerboseMessage(String.format("%s %s %s match pattern %s", prefix, path, matches ? "does" : "doesn't", pattern));
}
return matches;
}
}

Expand Down Expand Up @@ -1055,14 +1065,14 @@ static boolean processJarManifestMainAttributes(Path jarFilePath, BiConsumer<Pat
}
}

void handleMainClassAttribute(Path jarFilePath, Attributes mainAttributes) {
void handleMainClassAttribute(Path filePath, Attributes mainAttributes) {
String mainClassValue = mainAttributes.getValue("Main-Class");
if (mainClassValue == null) {
NativeImage.showError("No main manifest attribute, in " + jarFilePath);
NativeImage.showError("No main manifest attribute, in " + filePath);
}
String origin = "manifest from " + jarFilePath.toUri();
String origin = "manifest from " + filePath.toUri();
addPlainImageBuilderArg(NativeImage.injectHostedOptionOrigin(oHClass + mainClassValue, origin));
String jarFileName = jarFilePath.getFileName().toString();
String jarFileName = filePath.getFileName().toString();
String jarSuffix = ".jar";
String jarFileNameBase;
if (jarFileName.endsWith(jarSuffix)) {
Expand Down Expand Up @@ -1111,6 +1121,10 @@ private int completeImageBuild() {
addPlainImageBuilderArg(NativeImage.oR + enablePrintFlagsWithExtraHelp + "=" + printFlagsWithExtraHelpOptionQuery);
}

if (jarOptionMode) {
addCustomImageClasspath(jarFilePath);
}

if (shouldAddCWDToCP()) {
addImageClasspath(Paths.get("."));
}
Expand Down Expand Up @@ -1778,6 +1792,10 @@ void setDiagnostics(boolean val) {
}
}

void setJarFilePath(Path jarFilePath) {
this.jarFilePath = jarFilePath;
}

void setJarOptionMode(boolean val) {
jarOptionMode = val;
}
Expand Down

0 comments on commit 682856d

Please sign in to comment.