Description
Spring (MVC and Webflux) has a ResourceResolver
abstraction that can be used to resolve the versions in webjars, avoiding the need to maintain the version explicitly in 2 or more places (build file and HTML source). E.g. (from Petclinic):
<script src="/webjars/jquery/jquery.min.js"></script>
Resolves to classpath:/META-INF/resources/webjars/jquery/<version>/jquery.min.js
at runtime.
Spring Boot carries the responsibility of configuring the resource resolver, and currently it uses the webjars-locator-core
(https://github.com/webjars/webjars-locator-core) library to do that, so version resolution only works if that library is on the classpath. The WebJarsAssetLocator
from that library has a very broad and powerful API for locating files inside webjars, but there are some issues, namely:
- It is fairly inefficient, since it scans the whole
/META-INF/resources/webjars
classpath on startup (in a constructor!). - It has 2 awkward dependencies (github classpath scanner and jackson)
- It doesn't work in a native image (Help webjars locator to find assets in /META-INF/resources/webjars spring-attic/spring-native#157) because of the classpath scanning
But we don't need webjars-locator-core
to just do version resolution, which is all Spring Boot offers, because webjars have a very well-defined structure. They all have a pom.properties
with the version in it, and they only use a handful of well-known group ids, so they are easy to locate. It might be a good idea to implement it in Framework, since it is so straightforward and only depends on reading resources from the classpath.
All of the issues above could be addressed just by providing a simpler version resolver natively (and configuring the resource config in a native image with a hint).