JPragma Blog

Pragmatic Developers, Elegant Software

Static content and webjars in Micronaut applications

leave a comment »

Sometimes I need to add a very simple UI to my Micronaut applications. Lightweight javascript libraries such as JQuery or AngularJS are quite convenient for this purpose. Traditionally CDNs are used to reference and download these libraries at runtime, but I prefer to use webjars. This way my app will continue to work even if there is no internet connection or if certain CDN is blocked by e.g. corporate network policies.

Step 1

Add dependency to the webjar. In gradle this would look like:

runtimeOnly('org.webjars:jquery:3.5.1')

Step 2

Add following configuration to application.yml

micronaut:
  application:
    name: myapp
  router:
    static-resources:
      default:
        paths:
          - "classpath:public"
          - "classpath:META-INF/resources/webjars"

Step 3

Create static resources (html, js, css) under src/main/resources/public

Step 4

Reference webjar javascript library from html

<head>
    <script src="jquery/3.5.1/jquery.min.js"></script>
</head>

Written by isaaclevin

February 28, 2021 at 4:39 pm

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: