WebJars in Spring MVC

Last week I announced the WebJars project that allows you to specify your web libraries (JavaScript, CSS, etc) as dependencies in your Java web applications. With some help from Jeremy Grelle I was able to get a simple WebJars Spring MVC example working.

First you will need to add the WebJars repository to your build. For Maven, just add the following to your “pom.xml” build file:


Then add a WebJar dependency, like Twitter Bootstrap:


Then you need to add a resource handler to Spring MVC that maps requests from a given path to files in the classpath. The WebJars are all inside of a “public” directory. If you are using Java configuration in Spring then you would do the following:

public class WebConfig extends WebMvcConfigurerAdapter {
  public void addResourceHandlers(ResourceHandlerRegistry registry) {

Or if you are using XML config then add the following to your Spring config:

<mvc:resources mapping="/public/**" location="classpath:/public/"/>

Then you can reference a WebJar’s assets with something like:

<link rel='stylesheet' media='screen' href='/public/stylesheets/bootstrap.min.css'>

How easy is that? Now you are managing your web libraries as versioned dependencies and you even get transitive dependencies! Check out the full source code for this example on GitHub: https://github.com/jamesward/spring_webjars_demo

Right now there are only a couple of WebJars so if you need something else then simply request a new one via a new issue on GitHub.

Please let me know what you think about this. Thanks!

  • Daniel

    Great! But my unmentionables are in a right old state. Next can you work on a library plugin for Spring MVC to manage my sock drawer?

  • nicolas de loof

    Hum, so you compensate the lack of dependency management support in maven-war-plugin for javascript / css dependencies by adding (yet another) servlet-based request handling, that will be far less efficient tnat letting apache frontend handle static resources or a CDN to deliver common javascript libraries… is this really the best option ?

    • I’d like my application to be completely self contained including the static assets.  Then if a CDN can be used to speed up access to those assets, great!  I have an example of how to set that up in an article I’m working on: https://github.com/jamesward/jaxrsbars

  • Great stuff James. 

  • Phil Webb

    Nice.  If you are not using Spring or Play and are happy with a bit more typing you can even overlay these resources directly into your WAR to access them in the classic way.


    It’s a shame that the maven-war-plugin does not support transitive overlays.

    • Wow!  Nice!  I didn’t know that was possible.  Thanks Phil.

    • Frank Wienberg

      I wouldn’t recommend using war overlays. The result is that web  resources would be unpacked and re-packed over and over in your build process, which is unnecessarily time- and space-consuming. Because transitive dependencies do not work, you also cannot use Maven’s dependency management—all dependencies are hard-wired in a war.As detailed in http://blog.jangaroo.net/2012/06/managing-javascript-libraries-with.html, using the Servlet 3 feature that resources can be placed under ‘META-INF/resources’ inside any jar in ‘WEB-INF/lib’, there is no need to define a controller or Servlet. You can use normal jar dependencies, which work perfectly also for the transitive case: Maven’s standard war packaging automatically collects all transitive jar dependencies in ‘WEB-INF/lib’. And you remain independent from Spring or Play—just use any up-to-date Java web server or add another step to the build process that re-packages the final Java web app into a static one (see comment in the blog post and update of the example project on github).

  • Cool stuff, James!

  • Barry Pitman

    Awesome, thanks a lot!

  • Mateusz

    I have an issue linking gif and jpg files from css, they point to root and in root there is nothing.

    Other than that I like WebJars a lot!

    • Can you provide more details on how you are linking to these assets from css?

  • Pimentadev

    Hi, James!

    Great article, but unfortunately this solution isn’t working for me.
    I’m using Tomcat7 inside Eclipse and the CSS/JS files aren’t found by my HTML pages.
    I already changed the web.xml to v3.0 and the facets of the project too.
    Also tried to run it out of Eclipse, directly with Tomcat7, but nothing.

    I’m using SpringMVC and already made the proper configuration ( or ).

    Is there anything else that I need to do to make it work? Could you help me, please?

    Thanks a lot and congratulations again!

  • Issam

    I have no code completion for css, js when using an IDE with Webjars.
    Do you have any workaround to get code completion on IDE like Intellij IDEA?

  • Sergii Poddyachiy

    Thank you buddy. I almost gave up to find XML configuration in internet

  • Marijan Milinovic

    correct me if I am wrong…this webjar dependencies and java/xml config to use online files (css and js) to put them in project jar and you still has to manually write url to your files (css and js), manually create folders for that files in project and manually write it down in html/jsp or whatever?
    so, why I don’t manually put web url of that online files, version i want (like in dependency) or version-agnostic?
    let say http://code.jquery.com/jquery-latest.min.js