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 434c20d commit ac0f115
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,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 @@ -320,7 +322,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 moduleOptionMode = false;
private boolean dryRun = false;
private String printFlagsOptionQuery = null;
Expand Down Expand Up @@ -689,9 +690,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 @@ -1056,14 +1066,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 All @@ -1076,15 +1086,15 @@ void handleMainClassAttribute(Path jarFilePath, Attributes mainAttributes) {
}
}

void handleClassPathAttribute(Path jarFilePath, Attributes mainAttributes) {
void handleClassPathAttribute(Path filePath, Attributes mainAttributes) {
String classPathValue = mainAttributes.getValue("Class-Path");
/* Missing Class-Path Attribute is tolerable */
if (classPathValue != null) {
for (String cp : classPathValue.split(" +")) {
Path manifestClassPath = ClasspathUtils.stringToClasspath(cp);
if (!manifestClassPath.isAbsolute()) {
/* Resolve relative manifestClassPath against directory containing jar */
manifestClassPath = jarFilePath.getParent().resolve(manifestClassPath);
manifestClassPath = filePath.getParent().resolve(manifestClassPath);
}
/* Invalid entries in Class-Path are allowed (i.e. use strict false) */
addImageClasspathEntry(imageClasspath, manifestClassPath, false);
Expand Down Expand Up @@ -1112,6 +1122,10 @@ private int completeImageBuild() {
addPlainImageBuilderArg(NativeImage.oR + enablePrintFlagsWithExtraHelp + "=" + printFlagsWithExtraHelpOptionQuery);
}

if (jarOptionMode) {
addCustomImageClasspath(jarFilePath);
}

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

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

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

0 comments on commit ac0f115

Please sign in to comment.