ClassLoader Reloading - Class Loader based reloading is a popular solution used by most JVM frameworks, however it once again can lead to cached / stale classes, memory leaks and weird errors if the incorrect classloader is used at any moment.
Given the problems with existing solutions and a lack of a way built into the JVM to reload changes, the safest and best solution to reloading, and the one recommended by the Micronaut team, is to use automatic application restart via third-party tool.
Micronaut’s startup time is fast and automatic restart leads to a clean slate without potential hard to debug problems cropping up or memory leaks.
Using the Micronaut Maven Plugin
Every time you change a class Micronaut’s plugin will automatically restart the server.
For Gradle restart you can create a new application with the feature already configured using the CLI with the command mn create-app myapp --features file-watch
This essentially introduces the following configuration to application.yml
:
File Watch Configuration
Once this is in place to have Gradle automatically restart on any change add run the following command:
Using Gradle for Automatic Restart
Every time you make a change to class or resources Gradle will recompile and restart the application.
The native JVM implementation of the interface for Mac OS X using polling which is slow. To improve file watch performance add the following dependencies to your build if you are using OS X:
Gradle - Configuring Native File Watch on Mac OS X