Image for post
Image for post

Spring Boot: Webjars — bundle your favourite JS library easily

David Marko
Jan 30, 2017 · 2 min read

Probably every web application depends on some JavaScript library on CSS file. We have our own such files, but many are third party coming from other developers. Shared knowledge is nice. To include such third party library usually means to find the proper version, download it, unpack it and extract meaningful parts and put it inside out application structure. No way with web jars!

To bundle JS/CSS libraries similar way how we include java libraries into Spring Boot project, we can easily reference them inside build.gradle file (we are talking about Gradle based Spring Boot projects here, maven is quite similar). As an example we can include jQuery and Twitter Bootstrap into our project. To do so, simply open web site and find appropriate library and select version you require.

Image for post
Image for post

As we are running Gradle based project, we take gradle lib references and put them inside build.gradle the same way as we reference third party java libraries.


If we are editing Spring Boot project under IntelliJ with Gradle plugin, the project is getting updated and proper packages are downloaded and expanded. Ok, now we have both third party libraries somewhere under our project and we need to reference them in HTML. And this final part is quite easy as well. If we use Thymeleaf as a template engine, we can simply reference jQuery like this:

<script th:src="@{/webjars/jquery/2.2.1/jquery.min.js}" ></script>

Everything is very straightforward and when we decide to upgrade to latest JQuery or Twitter Bootstrap after some time, we just change appropriate version number in build.gradle file.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store