Including Webjars in IntelliJ Tomcat WAR Artifact

I recently started using Webjars with my NixMash Spring project on GitHub, specifically JQuery, Bootstrap and Bootswatch for now. Everything was going great. $gradle mvc:bootRun and I was enjoying pretty pages with Bootstrap and all that good jQuery stuff. Then I deployed a Spring Web Artifact with a Tomcat server in IntelliJ and realized my webjars were not in my IntelliJ WAR artifact. No more pretty web pages. My /webjar paths were spitting 404s.

A Few Webjar Generalities

If you read my post on adding Webjars to your Spring MVC project, you’ll know that it’s pretty easy to configure. The relationship between the org.webjar dependency JARs and how they end up in your WAR, however, can be a bit confusing.

It’s helpful to know that as long as the Webjars JARs are in your WAR’s WEB-INF/lib folder you’re good to go. It’s getting them in there that’s the trick.

When you create a WAR in Gradle, that gets taken care of. If you were to deploy that WAR as an External Resource in your IntelliJ Tomcat configuration, the Webjars would be included. But if deploying the IntelliJ-created WAR Artifact the Webjars are nowhere to be found.

A Matter of Adding the Webjar JARs to the IntelliJ Artifact

When IntelliJ builds a project and creates artifacts it does so in the project’s /classes folder. When Gradle does a build it does it in the module /build folder. Two separate locations. IntelliJ doesn’t know about those Webjars you added in your build.gradle dependencies list. You’re not going to find those libraries in your project’s External Libraries listing in the Project Window. So it’s a matter of adding the Webjar JARs to your IntelliJ Module Artifact.

Where Be Those Webjars?

A good place to start is knowing where those Webjar JARs reside. That’s going to be in your Maven Repository, typically /home/youraccount/.m2/repository/org/webjars for us Linux folk. You can either track them down in the repository  or download them again from Maven Central. Then store them in a /java/jar reference folder or somewhere you’ll remember on your system.

Once we have the Webjar JARs we can add them as IntelliJ project libraries. Notice “bootstrap-3.3.4” and “bootswatch-3.3.4+1” at the top of the list.

Finally we add them to the artifact lib folder as shown below. If you select the Artifacts area here IntelliJ will let you know that you’re missing dependencies in your WAR Artifact and ask if you want it fixed. Pretty cool. The point is that once your Webjars are present in your WAR Artifact’s lib folder you are good to launch Tomcat and deploy the WAR that IntelliJ built.

Launch Tomcat with the IntelliJ WAR Artifact and you’ve got pretty again.