Automatic restart

There are several tools which can speed up the development process. Using JRebel, spring-boot-devtools or HotswapAgent allows you to see changes without application restart.

JRebel

To try JRebel for free there is a 2 weeks trial. To start using it follow the instructions for your IDE: https://zeroturnaround.com/software/jrebel/quickstart/intellij/ or https://zeroturnaround.com/software/jrebel/quickstart/eclipse/

JRebel has integration with a lot of frameworks, in particular with spring-boot. It allows to substitute modified and recompiled classes into classpath without the application restart.

Spring-boot-devtools and Live Reload

To activate spring-boot-devtools is needed to:

Add spring-boot-devtools dependency

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-devtools</artifactId>
  4. <optional>true</optional>
  5. <scope>runtime</scope>
  6. </dependency>

Fork the process used to run the application by changing spring-boot-maven-plugin configuration

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. <version>${spring-boot.version}</version>
  5. <configuration>
  6. <fork>true</fork>
  7. </configuration>
  8. </plugin>

Create and place into the src/main/resources/META-INF the following spring-devtools.properties

  1. restart.include.flow=/flow.*\.jar
  2. restart.include.vaadin=/vaadin.*\.jar

This means we explicitly define that such jars are unmodifiable and they shouldn’t be reloaded on each application restart. More info at Customizing the restart classloader.

Optionally you might want to avoid the data generator to be run on each single reload, therefore, make H2 database store entities in file-system instead of in memory by adding the following lines to the src/main/resources/application.properties

  1. spring.datasource.url=jdbc:h2:file:~/bakery-test-data
  2. spring.jpa.hibernate.ddl-auto=update

To trigger the restart, it is needed to update classpath. In Eclipse it can be done automatically after saving modified file. In IntelliJ IDEA can be done manually Build → Build Project

Live reload is supported and browser extensions can be found at http://livereload.com/extensions/.

HotswapAgent

To setup HotswapAgent follow the tutorial.

  • Start with downloading and installing alternative JVM. Don’t forget to run the installer with administrator privileges.
  1. java -jar installer-name.jar
  • Download manually the HotswapAgent or install the IDE plugin (if you are using IntelliJ IDEA)

  • Create Run/Debug configuration which will run the HotswapAgent for IntellijIDEA or Eclipse.

For running the app through the HotswapAgent in a Debug session, add this line to the JVM parameters

  1. -XXaltjvm=dcevm -javaagent:PATH_TO_AGENT/hotswap-agent.jar

But if you rather a Run session, you need to set autoHotswap flag in the parameters

  1. -XXaltjvm=dcevm -javaagent:PATH_TO_AGENT/hotswap-agent.jar=autoHotswap=true
Note
For IntelliJ IDEA there is a HotswapAgent plugin which works with debug session only. It doesn’t support the autoHotswap flag. It’s needed to setup Run/Debug configuration manually for running without debug session.

If you prefer to run application via command line, you just need to set the JVM parameters via the MAVEN_OPTS environment variable:

  1. MAVEN_OPTS="-XXaltjvm=dcevm -javaagent:PATH_TO_AGENT/hotswap-agent.jar=autoHotswap=true" mvn spring-boot:ru

Updated 2021-03-08  Edit this article