Micronaut-core: Add support for live reloading

Created on 30 May 2018  路  14Comments  路  Source: micronaut-projects/micronaut-core

A lot of modern frameworks now support live reloading when assets are modified.

It would be amazing to have something similar for Micronaut since this dramatically improves the developement workflow, one can make a code change and immediately test it without having to CTRL-C and start the app again.

I鈥檝e seen the java framework Rapidoid implement a live reloading when some classes are changed.

in progress enhancement

Most helpful comment

There are various options for this:

  • JVM agent - use Spring Loaded or JRebel by configuring the agent (slows down application performance) but requires few changes to Micronaut itself
  • Classloader based reloading - Most frameworks take this approach. Increases complexity and can result in class leaks / cached class problems
  • Build Tool Reloading - Get the build tool to restart the server. This requires custom Gradle and Maven plugins which we would like to avoid

All of the above have their pros and cons. Maybe the least evil is ClassLoader based reloading. We will consider one of them.

However, since Micronaut runs so fast you can load your application in a unit test and run the test continuously within your IDE which is my personal favored approach.

All 14 comments

There are various options for this:

  • JVM agent - use Spring Loaded or JRebel by configuring the agent (slows down application performance) but requires few changes to Micronaut itself
  • Classloader based reloading - Most frameworks take this approach. Increases complexity and can result in class leaks / cached class problems
  • Build Tool Reloading - Get the build tool to restart the server. This requires custom Gradle and Maven plugins which we would like to avoid

All of the above have their pros and cons. Maybe the least evil is ClassLoader based reloading. We will consider one of them.

However, since Micronaut runs so fast you can load your application in a unit test and run the test continuously within your IDE which is my personal favored approach.

Closing this for now; if anyone has other reloading solutions that we should consider please open a new issue for tracking those.

@ZacharyKlein Does Ratpack use spring loaded or gradle continuous build mode to achieve this?
Hot reloading is very convenient way for rapid development, IMO unit tests aren't convenient to be used in every test.

I agree with binsnaps. Hot reloading is very convenient way for rapid development. Unit tests are great but aren't the best option in every scenario.

For Micronaut 1.1 we have added support for automatic restarts which are preferable over using an agent or class loaders which have numerous disadvantages already discussed. The new feature is documented in

https://github.com/micronaut-projects/micronaut-core/blob/master/src/main/docs/guide/cli/reloading/automaticRestart.adoc

From a framework from Red Hat
https://quarkus.io/guides/getting-started-guide

quarkus:dev runs Quarkus in development mode. This enables hot deployment with background compilation, which means that when you modify your Java files your resource files and refresh your browser these changes will automatically take effect. This works too for resource files like the configuration property file. Refreshing the browser triggers a scan of the workspace, and if any changes are detected the Java files are recompiled and the application is redeployed; your request is then serviced by the redeployed application. If there are any issues with compilation or deployment an error page will let you know.

This will also listen for a debugger on port 5005. If your want to wait for the debugger to attach before running you can pass -Ddebug on the command line. If you don鈥檛 want the debugger at all you can use -Ddebug=false.

I agree that something similar to Quarkus dev mode would be very much welcomed. Out of curiosity, are they using a Build Tool Reloading approach ?

yup they built custom Maven and Gradle plugins. The frameworks are very different. Quarkus uses byte code enhancement and processes the byte code modifying it as necessary so build plugins are required to use it. Micronaut doesn't modify byte code in a build step. It processes the sources using annotation processors and generates additional byte code so the only reason for us to build Maven and Gradle plugins would be to support this feature, otherwise they are simply unnecessary with Micronaut.

Now if the build tool supports continuously restarting the server then we don't even need any special build time plugins. This is already working for Gradle. So to get reloading working in Gradle just do:

$ mn create-app myapp --features file-watch
$ cd myapp
$ ./gradlew run --continuous

Unfortunately this a Gradle specific feature and Maven users are out of luck. So really all we are left with then is the lack of a Maven plugin. We may at some point write one just for this purpose, but it does seem kind of ridiculous to have to write a Maven plugin just for this case. I personally write tests and run them so don't miss the feature and never use file watch on either Gradle or Maven when I am building Micronaut apps, but ok I understand some peoples workflows are different.

Take note the --features file-watch, when creating a new app.
This guide is great to setup your existing app with hot-reloading: https://github.com/micronaut-projects/micronaut-core/blob/master/src/main/docs/guide/cli/reloading/automaticRestart.adoc

If i create analitic app, then me need load big data in memory. And create from data my struct.
Time import data very long, about ten seconds. Then restart app will be at ten seconds

Take note the --features file-watch, when creating a new app.
This guide is great to setup your existing app with hot-reloading: https://github.com/micronaut-projects/micronaut-core/blob/master/src/main/docs/guide/cli/reloading/automaticRestart.adoc

Hi, I don't know if anyone has tryied this guide?, and even though Micrount has this same guide in their documentation..for me, it's NOT WORKING. Maybe I'm doing something wrong.

I tested it with gradle because that's what I have to use. So haven't done it with maven.

I tryied it in ubuntu.

| Micronaut Version: 1.1.4
| JVM Version: 1.8.0_111

Has anyone tryied or tested anything different so we can build continuously?

I mean, in Spring we had Devtools and you could "./gradlew bootRun" in one terminal and "./gradlew build --continuous" in another and the server always restarted with changes in the code. And it is super duper comfortable to code like that.

I cannot find any more information or a clear example. Please help

OK, this is a bit of necro-posting, but it is better to post here rather than open a specific ticket.

Does mn:run support debugging in some way?!

I'm testing it now on intellij 2020. it recompiles if I made changes so it is great, but every time I try to attacha debugger, intellij shows the processes as read-only.

any hints?! thank you!

Was this page helpful?
0 / 5 - 0 ratings