Description
Andy Wilkinson opened SPR-14936 and commented
PathMatchingResourcePatternResolver behaves differently depending on the file extension of a Spring Boot executable archive that's been launched with java -jar
. If the archive is a .jar
file duplicate resources will be found, whereas if the archive is a .war
file they will not. This is due to the logic in addAllClassLoaderJarRoots
that provides special treatment for .jar
files.
I'll attach an application that reproduces the problem.
If you package and run it
mvn clean package && java -jar duplicate-resources-0.0.1-SNAPSHOT.jar
You should see the following output:
jar:file:/Users/awilkinson/duplicate-resources-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/a.zzz
jar:file:/Users/awilkinson/duplicate-resources-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/nested/b.zzz
jar:file:/Users/awilkinson/duplicate-resources-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes/a.zzz
jar:file:/Users/awilkinson/duplicate-resources-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes/nested/b.zzz
Note that there are two URLs for a.zzz
and b.zzz
, one found via the nested BOOT-INF/classes
"archive" and the other found via the jar root. We only want the entries found via the nested archive.
If you run it as a .war
file:
cp duplicate-resources-0.0.1-SNAPSHOT.jar duplicate-resources-0.0.1-SNAPSHOT.war && java -jar duplicate-resources-0.0.1-SNAPSHOT.war
The duplicates are gone and we get the desired result:
jar:file:/Users/awilkinson/duplicate-resources-0.0.1-SNAPSHOT.war!/BOOT-INF/classes!/a.zzz
jar:file:/Users/awilkinson/duplicate-resources-0.0.1-SNAPSHOT.war!/BOOT-INF/classes!/nested/b.zzz
I'd like the behaviour to be consistent, irrespective of the file extension that's used for the archive passed to java -jar
.
One final data point. If the archive is unpacked:
mkdir unpacked && cd unpacked && unzip ../duplicate-resources-0.0.1-SNAPSHOT.jar
And then run:
java -cp . org.springframework.boot.loader.JarLauncher
The duplicates do not occur:
file:/Users/awilkinson/unpacked/BOOT-INF/classes/a.zzz
file:/Users/awilkinson/unpacked/BOOT-INF/classes/nested/b.zzz
I suspect this is because the URLs are identical, i.e. they do not have the subtle /
vs !/
difference. This may give us an avenue to explore for fixing the problem in Spring Boot, but I'd like this to be investigated on the Framework side too as the file extension-specific behaviour is rather surprising.
Affects: 4.3.4
Attachments:
- duplicate-resources.zip (2.77 kB)
Issue Links:
- PathMatchingResourcePatternResolver provides duplicate resources with relative URL [SPR-14934] #19501 PathMatchingResourcePatternResolver provides duplicate resources with relative URL