From c693b2bd8cfa7c10b2791aea58f26d55be548c33 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 12 Jul 2024 19:17:17 +0200 Subject: [PATCH] Add support for webjars-locator-lite This is a follow-up to spring-projects/spring-framework#27619 This commit adds support for "org.webjars:webjars-locator-lite" for enabling the statis resources chain. As of this commit, support for "org.webjars:webjars-locator-core" is deprecated for obvious performance reasons. Closes gh-40146 --- .../web/ConditionalOnEnabledResourceChain.java | 7 +++++-- .../web/OnEnabledResourceChainCondition.java | 10 ++++++++-- .../spring-boot-dependencies/build.gradle | 7 +++++++ .../antora/modules/reference/pages/web/servlet.adoc | 5 +---- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ConditionalOnEnabledResourceChain.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ConditionalOnEnabledResourceChain.java index 3a3467b5e6..62b8760ec8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ConditionalOnEnabledResourceChain.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ConditionalOnEnabledResourceChain.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,10 @@ import org.springframework.context.annotation.Conditional; /** * {@link Conditional @Conditional} that checks whether the Spring resource handling chain * is enabled. Matches if {@link WebProperties.Resources.Chain#getEnabled()} is - * {@code true} or if {@code webjars-locator-core} is on the classpath. + * {@code true} or if one of {@code "org.webjars:webjars-locator-core"}, + * {@code "org.webjars:webjars-locator-lite"} is on the classpath. + *

+ * Note that support for {@code "org.webjars:webjars-locator-core"} is deprecated. * * @author Stephane Nicoll * @since 1.3.0 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java index c4ce3b4ea9..e457f8a31b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/OnEnabledResourceChainCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,12 +32,15 @@ import org.springframework.util.ClassUtils; * @author Stephane Nicoll * @author Phillip Webb * @author Madhura Bhave + * @author Brian Clozel * @see ConditionalOnEnabledResourceChain */ class OnEnabledResourceChainCondition extends SpringBootCondition { private static final String WEBJAR_ASSET_LOCATOR = "org.webjars.WebJarAssetLocator"; + private static final String WEBJAR_VERSION_LOCATOR = "org.webjars.WebJarVersionLocator"; + @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment(); @@ -47,10 +50,13 @@ class OnEnabledResourceChainCondition extends SpringBootCondition { Boolean match = Chain.getEnabled(fixed, content, chain); ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnEnabledResourceChain.class); if (match == null) { + if (ClassUtils.isPresent(WEBJAR_VERSION_LOCATOR, getClass().getClassLoader())) { + return ConditionOutcome.match(message.found("class").items(WEBJAR_VERSION_LOCATOR)); + } if (ClassUtils.isPresent(WEBJAR_ASSET_LOCATOR, getClass().getClassLoader())) { return ConditionOutcome.match(message.found("class").items(WEBJAR_ASSET_LOCATOR)); } - return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_ASSET_LOCATOR)); + return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_VERSION_LOCATOR)); } if (match) { return ConditionOutcome.match(message.because("enabled")); diff --git a/spring-boot-project/spring-boot-dependencies/build.gradle b/spring-boot-project/spring-boot-dependencies/build.gradle index 07272c6901..cdf3ec04a9 100644 --- a/spring-boot-project/spring-boot-dependencies/build.gradle +++ b/spring-boot-project/spring-boot-dependencies/build.gradle @@ -2206,6 +2206,13 @@ bom { ] } } + library("WebJars Locator Lite", "1.0.0") { + group("org.webjars") { + modules = [ + "webjars-locator-lite" + ] + } + } library("WebJars Locator Core", "0.59") { group("org.webjars") { modules = [ diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/servlet.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/servlet.adoc index ff0e6388d3..11557ee939 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/servlet.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/servlet.adoc @@ -145,13 +145,10 @@ Although this directory is a common standard, it works *only* with war packaging Spring Boot also supports the advanced resource handling features provided by Spring MVC, allowing use cases such as cache-busting static resources or using version agnostic URLs for Webjars. -To use version agnostic URLs for Webjars, add the `webjars-locator-core` dependency. +To use version agnostic URLs for Webjars, add the `org.webjars:webjars-locator-lite` dependency. Then declare your Webjar. Using jQuery as an example, adding `"/webjars/jquery/jquery.min.js"` results in `"/webjars/jquery/x.y.z/jquery.min.js"` where `x.y.z` is the Webjar version. -NOTE: If you use JBoss, you need to declare the `webjars-locator-jboss-vfs` dependency instead of the `webjars-locator-core`. -Otherwise, all Webjars resolve as a `404`. - To use cache busting, the following configuration configures a cache busting solution for all static resources, effectively adding a content hash, such as ``, in URLs: [configprops,yaml]