Skip to content

Resources in Spring Boot artifacts return the wrong URL #505

@michael-simons

Description

@michael-simons

Hi Luke.

This is basically the sequel to #435. A user has brought to my attention that the release 4.8.91 (see https://github.com/classgraph/classgraph/releases/tag/classgraph-4.8.91) returns invalid class path element URIs for packaged resources in this michael-simons/neo4j-migrations#179 ticket.

Find a reproducer attached. NOTE: The thing must be packaged with mvn clean package and executed with java -jar target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar, running from the IDE is not enough. Commands are shown below so that you don't have to change anything in the pom to switch versions:

Reproducer: classgraph_resources_issue.zip

The core issue is demonstrated between here:

try (ScanResult scanResult = new ClassGraph()
	.whitelistPaths("foo").scan()) {
	List<Resource> resources = scanResult.getResourcesWithExtension("cypher")
		.stream()
		.collect(Collectors.toList());

	resources.forEach(System.err::println);
	if (resources.size() > 1) {
		throw new RuntimeException(":(");
	}

	System.err.println("Reading via Resource#open");
	try (InputStream in = resources.get(0).open()) {
		System.err.println(readInputStream(in));
	} catch (IOException e) {
		e.printStackTrace();
	}

	System.err.println("Reading via Resource#getURL().openStream()");
	System.err.println("URL is " + resources.get(0).getURL());
	try (InputStream in = resources.get(0).getURL().openStream()) {
		System.err.println(readInputStream(in));
	} catch (IOException e) {
		e.printStackTrace();
	}
}

Classgraph 4.8.90

Run with

mvn clean package -Dclassgraph.version=4.8.90 && java -jar target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar

Expected output

jar:file:/Users/msimons/Downloads/cg/classgraph_resources_issue/target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes/foo/a.cypher
Reading via Resource#open
asd
Reading via Resource#getURL().openStream()
URL is jar:file:/Users/msimons/Downloads/cg/classgraph_resources_issue/target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes/foo/a.cypher
asd

Classgraph 4.8.102

Run with

mvn clean package -Dclassgraph.version=4.8.102 && java -jar target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar

Resolving fails (notice the double BOOT-INF). I expect that getURL returns something that is immediate usable, too.

jar:file:/Users/msimons/Downloads/cg/classgraph_resources_issue/target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/BOOT-INF/classes/foo/a.cypher
Reading via Resource#open
asd
Reading via Resource#getURL().openStream()
URL is jar:file:/Users/msimons/Downloads/cg/classgraph_resources_issue/target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/BOOT-INF/classes/foo/a.cypher
java.io.FileNotFoundException: JAR entry BOOT-INF/classes/foo/a.cypher not found in /Users/msimons/Downloads/cg/classgraph_resources_issue/target/classgraph_resources_issue-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions