Using web-jar in Spring boot, Ensure IDE automatically Downloads POM dependencies

Using web-jar in Spring boot, Ensure IDE automatically Downloads POM dependencies

While learning spring boot, I came across feature known as web-jars. WebJars are very helpful for static content management. I.e you don’t have to manually map in your CSS and JS file paths. WebJars, once dependency added in your project, they would download your dependencies and will put somewhere in webjar folder.

But when I added the webjar dependencies in my pom.xml file, Editor was suggestion Could not find dependencies for org.webjar. I was confuse what to do.
If you’re using Intellij goto File > Settings and search for Maven in the left top search box. Alternatively you can traverse Built, Execution, Deployment > Built Tools > Maven. Check the following image for your reference.

if you hover your mouse on that option and keep it for a while it should show you a tooltop saying
“Synchronize maven project model and IDEA project model each time when project pom.xml is changed”

So now whenever you change or add new dependencies to you pom.xml file, IDE will automatically download newly added dependencies and make it available for your project. Until the newly library is downloaded and imported it will remain in red, once imported it will turn normal color like others.

Now you’ll question how to point this static resources in our html file. Simple, follow along the lib you’ve downloaded. for example suppose you’ve added dependencies in pom.xml for jquery 3.2.1

<dependency>
 <groupId>org.webjars</groupId>
 <artifactId>jquery</artifactId>
 <version>3.2.1</version>
</dependency>

Then you may followin your static file as

<script src=”webjars/jquery/3.2.1/jquery.min.js”>

As soon as you stat typing, is should show to auto-suggestions if you’re using Intellij IDE.

Now run your project and in the project view source you should be able to see as

http://localhost:8080/webjars/jquery/3.2.1/jquery.min.js

Sring boot manages the asset for you somewhere in its storage.

Hope you enjoyed. Do implement.