Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process custom image classpath after parsing options #3749

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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