Package org.webjars

Class WebJarVersionLocator

java.lang.Object
org.webjars.WebJarVersionLocator

@NullMarked public class WebJarVersionLocator extends Object
Helper Class to locate WebJar versions.

By default, this class only supports looking up official WebJars with the Maven group IDs of org.webjars.npm and org.webjars.

Custom WenJars can be registered by providing a META-INF/resources/webjars-locator.properties file.

Note: It is recommended, to add this file directly to the custom WebJar to ease the usage. But for WebJars not providing the file, you can add a webjars-locator.properties to your project.

Example file (multiple WebJars can be provided, one per line):


 mywebjar.version=3.2.1
 anotherwebjar.version=1.4.3
 

As the lookup of all webjars-locator.properties files happens during the construction of the class and the found versions are directly added to the cache, these property files can and will override versions that otherwise would be looked up by version(String).

When multiple webjars-locator.properties files contain a version for the same WebJar, the one that has been found first wins.

The class is thread safe.

  • Field Details

    • WEBJARS_PATH_PREFIX

      public static final String WEBJARS_PATH_PREFIX
      The path to where webjar resources live.
      See Also:
  • Constructor Details

    • WebJarVersionLocator

      public WebJarVersionLocator()
  • Method Details

    • fullPath

      public @Nullable String fullPath(String webJarName, String filePath)
      Builds the versioned path for a file of a WebJar within the standard WebJar classpath location (see WEBJARS_PATH_PREFIX).

      The path is built by prefixing the versioned path built by path(String, String) with the standard WebJars location classpath.

      See path(String, String) for a detailed explanation of how the versioned file path is built.

      Note: This method does not perform any checks if the resulting path references an existing file.

      Parameters:
      webJarName - The name of the WebJar, this is the directory in the standard WebJar classpath location, usually the same as the Maven artifact ID
      filePath - The path to the file within the WebJar
      Returns:
      The versioned path to the file in the classpath, if a version has been found, otherwise null
      See Also:
    • path

      public @Nullable String path(String webJarName, String filePath)
      Builds the versioned path for a file of a WebJar relative to the standard WebJar classpath location (see WEBJARS_PATH_PREFIX).

      The path is built by joining the webJarName, the known version and the filePath, if no version (from classpath checking) is known for the WebJar this method returns null.

      Note: In cases where the filePath parameter already starts with the known version of the WebJar, the version will not be added again. But it is recommended that you do NOT include a hard-coded version when looking up WebJar file paths.

      
       // returns "bootstrap/3.1.1/css/bootstrap.css"
       locator.path("bootstrap", "css/bootstrap.css");
      
       // returns "bootstrap/3.1.1/css/bootstrap.css" as well
       locator.path("bootstrap", "3.1.1/css/bootstrap.css");
      
       // returns null, assuming there is no "unknown" WebJar
       locator.path("unknown", "some/file.css");
       

      Note: This method does not perform any checks if the resulting path references an existing file.

      Parameters:
      webJarName - The name of the WebJar, this is the directory in the standard WebJar classpath location, usually the same as the Maven artifact ID
      filePath - The path to the file within the WebJar
      Returns:
      The versioned path relative to the standard WebJar classpath location, if a version has been found, otherwise null
      See Also:
    • version

      public @Nullable String version(String webJarName)
      This method tries to determine the available version for a WebJar in the classpath.

      For official WebJars, the version lookup is performed by checking for a pom.properties file for either NPM or PLAIN WebJars within META-INF/maven. The lookup result is cached.

      Custom WebJars can be registered by using a webjars-locator.properties file. See WebJarVersionLocator for details.

      Parameters:
      webJarName - The name of the WebJar, this is the directory in the standard WebJar classpath location, usually the same as the Maven artifact ID
      Returns:
      The version of the WebJar, if found, otherwise null
      See Also:
    • groupId

      public @Nullable String groupId(String webJarName)
      This method tries to determine the groupId for a WebJar in the classpath.

      For official WebJars, the version lookup is performed by checking for a pom.properties file for either NPM or PLAIN WebJars within META-INF/maven. The lookup result is cached.

      Custom WebJars can be registered by using a webjars-locator.properties file. See WebJarVersionLocator for details.

      Parameters:
      webJarName - The name of the WebJar, this is the directory in the standard WebJar classpath location, usually the same as the Maven artifact ID
      Returns:
      The groupId of the WebJar, if found, otherwise null
      See Also: