-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add support for webjars-locator.properties #15
Conversation
@@ -25,17 +28,22 @@ public class WebJarVersionLocator { | |||
private static final String NPM = "org.webjars.npm/"; | |||
private static final String PLAIN = "org.webjars/"; | |||
private static final String POM_PROPERTIES = "/pom.properties"; | |||
private static final String LOCATOR_PROPERTIES = "/locator.properties"; | |||
|
|||
private static final String CACHE_KEY_PREFIX = "version-"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to avoid duplication of the string
} | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("unable to load locator properties", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this exception should be rethrown, logged or just ignored (similar to version()
). For now I opted for throwing.
return value; | ||
}); | ||
return cache.computeIfAbsent(key, inspectableFunction); | ||
} | ||
} | ||
|
||
final WebJarVersionLocator webJarVersionLocator = new WebJarVersionLocator(new InspectableCache()); | ||
// enable inspection after webJarVersionLocator has been constructed, to ignore lookups caused by loading locator.properties | ||
shouldInspect.set(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My first Idea was to modifiy the assertEquals
calls for numLookups
but I think that this is a better solution.
} | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("unable to load locator properties", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this Exception should only happen if the the locator.properties
was found but could not be read. So we should be good if there is no locator.properties
- is that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, LOADER.getResources
returns an empty Enumeration
when there are no files found. I verified that by removing the locator.properties
from test resources.
Thanks for tackling this! Overall I think this looks good. Does this address everything you need for #13? |
Yeah, I am pretty sure, that this should be a proper solution for #13. Due to the fact, that all webjars should have thier content in I'm not sure about the performance impacts of the two variants, but maybe the |
Thanks! Since this is a change we'd like to have integrated into Spring, @dsyer want to review? |
If I’m reading it correctly this locator properties file kicks in whenever it is present (and mostly it would not be). So users don’t have to do anything unless they want to provide locator properties themselves, but libraries might do that for them most commonly. If all that is correct there’s nothing to be done to integrate with existing usage. |
I don't know if supporting GraalVM native images is in the scope of this library; if that's the case, I would suggest adding the following file {
"resources": {
"includes": [
{
"pattern": "\\QMETA-INF/webjars/locator.properties\\E",
"condition": {
"typeReachable": "org.webjars.WebJarVersionLocator"
}
}
]
}
} |
+1 for @bclozel proposal as well as doing a real test of a Spring application compiled to native to check this PR does not break native support (which was one of the goal of |
@dsyer Correct. As of now, the file would only be present if a user is adding it to support legacy webjars that would otherwise not be found, or by non-standard 3rd-party webjars. In theory, official webjars COULD provide the file going forward, but that is up to @jamesward or the webjars team to decide. Regarding native-image, I think if it is just about adding that json file, I would say we should go for it. but that again is up to james to decide. @bclozel @sdeleuze as I have no experience with graalvm/native-image at all, it would be awesome if one of you guys could help out with doing a native test for this. |
Thanks all! Yes, we definitely want to make sure this is all compatible with native image. So a test would be great. It's a bit of a lift and we should do it. In the meantime, question for @bclozel and @sdeleuze - if we include the resource-config, it is ok if the referenced file ( |
btw, thanks all! you're amazing and I really appreciate your support! |
Given the extent of the native image metadata, I'm not sure it's worth building a native image test in the CI. With upcoming GraalVM versions, I think you should be able to test things with a non-native binary and still check that your metadata is applied as expected. As for the |
My interpretation of the "doing a real test", was more like build a small app (or use an existing) and test if this works there. not a full CI test that runs always. although this could make sense to have this in long term to ensure compat/not breaking stuff. |
I gave testing this in a project a try and started with testing a project using After that I moved on and tried to get this working in GraalVM, or better said, get this to FAIL.
All this, without having the Again, I am not familiar with GraalVM so I might have done something "wrong" for it to work or fail in this case. I tested using Spring Boot My assumption is, that this might be related to the |
Thanks @dodgex for doing all this testing! Really good find with As for not needing the |
0e2cb76
to
5335fea
Compare
Amended the code and commit messages to use |
@bclozel @sdeleuze can you guys clarify if my assumption in #15 (comment) regarding the properties file in native is correct? I mean we can still add the |
Spring is not expected to add blindly resource files, so you should probably check the
Quickly checking on Petclinic, it looks like WebJars entries are indeed added:
I guess that's added via the values we configure for the resource handlers in Spring Boot, so indeed it looks like for Spring you should be good. So up to you to decide if you want to provide native support for |
Thank you for your quick response! :) @jamesward now it is up to you to decided, if the And I would say, as soon as you decide to add the file, it should include all entries to find webjars. not just the locator file. e.g. the stuff that is currently generated by the Spring AOT support as sdeleuze listed above in addition to the properties file. |
Thanks all! If there aren't any downsides to including the config, then let's do it. Currently I don't see any but let me know if I'm missing something. |
f9031b4
to
e4a929d
Compare
e4a929d
to
688013c
Compare
I added the JSON in 688013c, even though, this commit actually might be out of scope of the actual PR. But as we discussed it here i think it still fits and there is no need for another PR. I hope this is whatever is needed. I added an entry for the |
Thank you for all the work on this! I'm releasing |
Awesome, I am glad we got this together! :) |
Thank you all! |
With this PR I want to bring up a possible implementation for supporting legacy and custom webjars with webjars-locator-lite. I started the related discussion in #13.
Unfortunately there was no decision on how to implement this feature in the related issue, therefore I tried to come up with something based on the suggestions.
The Implementation provided by this PR looks for
META-INF/webjars/locator.properties
files on the classpath and then reads them into aProperties
instance. I decided to use a pattern ofWEBJAR_NAME.version
as key in the properties file to have the option to add other properties in the future (and be more specific about the purpose of the property -> providing a version). The value for each key has to be the version of the webjar as used in the resulting path.When reading the
locator.properties
the.version
suffix is removed and the given version is put into the cache. Before adding a given webjar version to the cache, it checks if the resulting path exists.The files are loaded in whatever order
LOADER.getResources()
returns them.With this change, it is possible to support any legacy (e.g. bower) or custom webjar, as long as the basic path structure is valid (
META-INF/resources/webjars/WEBJAR/VERSION/any/folder/or.file
)