WebJars work with most JVM-based containers and web frameworks. Using a WebJar requires:

  1. The WebJar needs to be a dependency of your application
  2. The WebJar needs to be in your application's running CLASSPATH
  3. Your container, web framework, or application needs to serve static assets from Jar files

Public CDN via jsDelivr

All of the WebJar contents are available on the public jsDelivr CDN. Just prefix //cdn.jsdelivr.net in front of your static asset URLs. For instance, if the URL you setup to jquery.js is /webjars/jquery/2.1.0/jquery.js then to use the CDN the URL would be: //cdn.jsdelivr.net/webjars/jquery/2.1.0/jquery.js

Web Framework Documentation

Instructions for Play 2.3 (Sample Source)

WebJars can be added as dependencies to an app by simply adding them to the build.sbt file. There is also a helper library named webjars-play that makes it easy to reference WebJar assets. Here is an example build.sbt file with webjars-play and the bootstrap WebJar as dependencies:
libraryDependencies ++= Seq(
  "org.webjars" %% "webjars-play" % "2.3.0-2",
  "org.webjars" % "bootstrap" % "3.1.1-2"
)

After changing a Play 2's dependencies you will need to restart Play.

In Play 2.3 WebJars are automatically available as static assets. If you have a route like:
GET         /assets/*file           controllers.Assets.at(path="/public", file)

Then a WebJar file like `bootstrap.css` is available at:
/assets/lib/bootstrap/css/bootstrap.css

The webjars-play helper library has a wrapper around the Play Static Asset Controller that can locate and serve WebJar assets. A new route to WebJarAssets needs to be added to the conf/routes file:
GET     /webjars/*file                    controllers.WebJarAssets.at(file)

To use Play 2's reverse routing to resolve the URL to an asset you can do the following in a template:
<link rel='stylesheet' href='@routes.WebJarAssets.at(WebJarAssets.locate("css/bootstrap.min.css"))'>
<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>

The WebJars contain versioned paths when accessed through the WebJarAssets controller. For local non-WebJar assets you should also setup a route for versioned assets via the sbt-digest asset compiler, like:
GET         /vassets/*file          controllers.Assets.versioned(path="/public", file: Asset)

WebJars have out-of-the-box support for RequireJS. To use it first include the RequireJS config in your page:
<script>
    @Html(org.webjars.RequireJS.getSetupJavaScript(routes.WebJarAssets.at("").url))
</script>

Then load RequireJS with a data-main for your JavaScript app:
<script data-main="@routes.Assets.versioned("javascripts/index.js")"
  src="@routes.WebJarAssets.at(WebJarAssets.locate("require.min.js"))"></script>

If you need to override the default RequireJS config you can use a lower-level RequireJS API to setup the config:
<script>
  var require = {
    callback: function() {
      // default requirejs configs
      @for(webJarJson <- org.webjars.RequireJS.getSetupJson(routes.WebJarAssets.at("").url).values()) {
        requirejs.config(@Html(webJarJson.toString));
      }

      // example custom requirejs config
      requirejs.config({
        paths: {
          jquery: "//code.jquery.com/jquery-1.11.1.min"
        },
        shim: {
          bootstrap: []
        }
      });
    }
  };
</script>

Instructions for Xitrum

Xitrum from 3.13 has built-in support for WebJars. If you have a dependency like this:

libraryDependencies += "org.webjars" % "underscorejs" % "1.6.0-3"

In you Scalate view template file, you can write like this (the below examples use Jade syntax):

script(src={webJarsUrl("underscorejs/1.6.0", "underscore.js", "underscore-min.js")})

Xitrum will automatically use underscore.js for development environment and underscore-min.js for production environment.

The result will look like this:

<script src="/webjars/underscorejs/1.6.0/underscore.js?XOKgP8_KIpqz9yUqZ1aVzw"></script>

If you want to use the same file for both environments:

script(src={webJarsUrl("underscorejs/1.6.0/underscore.js")})

For examples, see xitrum-new and xitrum-demos (online).

Instructions for Servlet 3

With any Servlet 3 compatible container, the WebJars that are in the WEB-INF/lib directory are automatically made available as static resources. This works because anything in a META-INF/resources directory in a JAR in WEB-INF/lib is automatically exposed as a static resource.

Maven Example (example app)

First add a WebJar as a dependency of your application in the pom.xml file, like:
<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

Then simply reference the resource like:
<link rel='stylesheet' href='webjars/bootstrap/3.1.0/css/bootstrap.min.css'>

Instructions for JSF

With JSF, the WebJars that are in the WEB-INF/lib directory are automatically made available as resource libraries. This works because WebJars are compatible with the JSF resource identifier format.

More Information about JSF resources

Maven Example (example app)

First add a WebJar as a dependency of your application in the pom.xml file, like:
<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

Then simply reference the resource like:
<h:outputStylesheet library="webjars" name="bootstrap/3.1.0/css/bootstrap.min.css" />

Instructions for Grails (example app)

Grails manages static resources (such as javascript and css files) with the resources plugin. The resources plugin manages static resources via modules, like the Jquery Module, that define via a config file which static resources a module requires. For a more details explanation see the Grails docs on using Static Resource.

The Grails Modules Manager plugin allows dependencies on web libraries to be declared in the Grails build configuration file, BuildConfig.groovy. It resolves theses dependencies and creates modules that can be used by the resources plugin. It does this by downloading the jar files from webjars.org and extracting the web libraries from the jar files. It then creates grails specific resource modules from these web libraries.

Steps for using the plugin:

  • Clone the modules manager plugin from
    https://github.com/groovydev/modules-manager-grails-plugin.git

  • Build the plugin:
    grails package-plugin

  • Install the plugin into your grails project from the file system - this assumes the plugin is in the same parent directory as the project:
    grails install-plugin ../modules-manager-grails-plugin/grails-modules-manager-0.2.1.zip

  • Define the javascript and css dependencies. In grails-app/config/BuildConfig.groovy add:
    dependencies {
        compile 'org.webjars:bootstrap:3.1.0'
    }

  • Run the refresh modules command which will download the necessary javascript and css dependencies:
    grails refresh-modules

  • Notice how the plugin added the file conf/ModulesBootstrapResources.groovy. This module config file is used by the resources plugin to define the module dependencies and static resources.

  • Add the module dependencies to the web page (see the example in views/index.gsp). This is done by adding the require tag to the html page to include the static resource modules. Also note that it is necessary to add the layoutResources tag twice to the page.

    In the head tag add:
    <head>
    <r:require modules="jquery, bootstrap"/>
    <r:layoutResources/>

    And then at the bottom of the page right before the body add:
    <r:layoutResources/>
    </body>

  • The resource manager then optimizes when the javascript is load by when it is need in the page and by default includes the javascript at the bottom of the page. For this reason it is necessary to put the javascript in a <r:script> tag so that the necessary dependencies will be included when the layoutResources tag is called. For example to use a jquery initialization function add the following to the page:
    <r:script>
    $(function (){ ... } 

Instructions for Dropwizard (example app)

With Dropwizard you can easily expose WebJars through the AssetsBundle. In your startup service's constructor setup the AssetsBundle to map static asset requests in a /META-INF/resources/webjars directory in JARs on the CLASSPATH to the /webjars URL, for example:
package org.webjars;

import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.bundles.AssetsBundle;
import com.yammer.dropwizard.config.Configuration;
import com.yammer.dropwizard.config.Environment;

public class MainService extends Service {

    public static void main(String[] args) throws Exception {
        new MainService().run(args);
    }

    private MainService() {
        super("sample-dropwizard");
        addBundle(new AssetsBundle("/META-INF/resources/webjars", 0, "/webjars"));
    }

}

Now you can reference a WebJar asset like:
<link rel='stylesheet' href='/webjars/bootstrap/3.1.0/css/bootstrap.min.css'>

Instructions for Spring MVC

Spring MVC makes it easy to expose static assets in JAR files using ResourceHandlers.

Maven Example (example app)

First add a WebJar as a dependency of your application in the pom.xml file, like:
<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

Then configure Spring to map requests for /webjars to the /META-INF/resources/webjars directory of all the JARs in the CLASSPATH. This can be done either via XML config:
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

Note In a Servlet 3 container this can be simplified to:
<mvc:resources mapping="/webjars/**" location="/webjars/"/>

Or Java config:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

}

Note In a Servlet 3 container the registry.addResourceHandler line can be simplified to:
registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");

Then reference a WebJar asset like:
<link rel='stylesheet' href='/webjars/bootstrap/3.1.0/css/bootstrap.min.css'>

Enhanced support for RequireJS

RequireJS is a popular implementation of the AMD specification - a means by which JavaScript applications can be modularised. The easiest way of thinking about AMD is that it is JavaScript's equivalent of package and import statements (or namespace and include statements depending on your preferences!). These instructions assume basic knowledge of RequireJS.

The webjars-locator library has built-in support for RequireJS. To setup RequireJS use the webjars-locator library like this:
@ResponseBody
@RequestMapping(value = "/webjarsjs", produces = "application/javascript")
public String webjarjs() {
    return RequireJS.getSetupJavaScript("/webjars/");
}
Note The RequestMapping must not be the same as given to the ResourceHandler
Note The url given to getSetupJavaScript has to be the url given to ResourceHandler and end with a /

This RequestMapping returns the setup code for your webjars and requirejs. It has to be included in your template before any JavaScript library that uses RequireJS like this:
<script data-main="/js/core.js" src="/webjarsjs"></script>
This loads the WebJars RequireJS configuration from webjars-locator and the RequireJS with a main JavaScript of js/app.

Underneath the covers each WebJar can have a RequireJS configuration file that sets up the modules, shims, exports, etc. These files is named webjars-requirejs.js and are automatically added to the page via the RequireJS.setup helper. Some of the WebJars may not be updated to the new WebJars RequireJS syntax so if you experience problems please file issues on the WebJars you are using.

Instructions for Apache Tapestry

Apache Tapestry makes it easy to expose static assets in JAR files using contributeClasspathAssetAliasManager.

Maven Example (example app)

First add a WebJar as a dependency of your application in the pom.xml file, like:
<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

Then configure the contributeClasspathAssetAliasManager in your AppModule to look for assets in META-INF/resources/webjars directories:
public class AppModule {
    public static void contributeClasspathAssetAliasManager(MappedConfiguration configuration) {
        configuration.add("webjars", "META-INF/resources/webjars");
    }
}

Then simply reference WebJars assets in your Tapestry templates like:
<link rel='stylesheet' media='screen'
    href='${asset:classpath:/META-INF/resources/webjars/bootstrap/3.1.0/css/bootstrap.min.css}'></link>
<script type='text/javascript'
    src='${asset:classpath:/META-INF/resources/webjars/jquery/1.9.0/jquery.min.js}'></script>

Instructions for Apache Wicket

The Wicket integration of Webjars uses a special IResourceFinder implementation to map Webjars resources.

Maven Example (example app)

First you have to add wicket-webjars as dependency to your application in the pom.xml file, like:
<dependencies>
    <dependency>
        <groupId>de.agilecoders.wicket.webjars</groupId>
        <artifactId>wicket-webjars</artifactId>
        <version>0.3.4</version>
    </dependency>
</dependencies>

And a WebJar dependency like:
<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>1.11.0</version>
    </dependency>
</dependencies>

Then configure your wicket application to map requests for /webjars and instances of IWebjarsResourceReference to the /META-INF/resources/webjars directory of all the JARs in the CLASSPATH. This can be done in Application.init:
/**
* @see org.apache.wicket.Application#init()
*/
@Override
public void init() {
   super.init();
   WicketWebjars.install(this);
}

Then simply reference the resource like:
<link rel='stylesheet' href='/webjars/jquery/1.11.0/jquery.js'>

Or add a Webjars*ResourceReference to your component:
@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);
    response.render(JavaScriptHeaderItem.forReference(
            new WebjarsJavaScriptResourceReference("jquery/1.11.0/jquery.js")));
}
                

To always use the most recent version of a WebJar asset, simply replace the version in path with the "current" string. When a resource name is resolved this string will be replaced with the most recent available version in classpath:
@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);

    // current will be replaced with "1.11.0"
    response.render(JavaScriptHeaderItem.forReference(
                    new WebjarsJavaScriptResourceReference("jquery/current/jquery.js")));
}

Instructions for Ring (example app)

Ring makes it easy to expose WebJars through the wrap-resource function. First add a Webjar as dependency to your application in the project.clj file, like:
:dependencies [[org.webjars/bootstrap "3.1.0"]]

Then change your wrapper sequence to setup wrap-resource to look for assets in /META-INF/resources directories in JARs on the CLASSPATH:
(def app
  (-> handler
    (wrap-resource "/META-INF/resources")))

(defn -main []
  (run-jetty app {:port (Integer/parseInt (or (System/getenv "PORT") "8080"))}))

Now you can reference a WebJar asset like:
<link rel='stylesheet' href='/webjars/bootstrap/3.1.0/css/bootstrap.min.css'>

Using clj-webjars

Alternatively you can use clj-webjars to simplify assets integration. By relying on wrap-webjars ring middleware you can reference a WebJar asset like:
<link rel='stylesheet' href='assets/css/bootstrap.min.css'>
The right asset will be transparently accessed and served with proper HTTP caching behavior.