Automatic Spring Boot project redeploy with DevTools

spring boot devtools

How many times during the day you recompile and restart your project? 10? 20? Probably even more. If you’re tired of waiting for your application to redeploy and don’t want to spend hundreds of dollars on JRebel, you can count on Spring Boot’s devs. Together with Boot they provide DevTools that automatically redeploys the project on classpath modification. I feel like this tool is too little popular and should reach wider audience.

Spring Boot DevTools

In order to allow Spring Boot to automatically reload files on the classpath, all we need to do is add another dependency to our pom/gradle file:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
</dependency>

If your application is running, stop it, recompile and redeploy. Now you can make changes to your classes, static files etc. and on recompilation Spring Boot will replace changed file beneath the surface.

Testing auto restarting Spring Boot project

I’ve chosen a small reactive service from one of my previous posts to be the project under test.

Normally, before applying Spring Boot DevTools, my project starts within ~7-8 seconds:restart spring boot without devtools

After adding the mentioned dependency, I can skip recompiling the whole project and just rebuild the file that’s been changed. Also rerunning JVM is not needed anymore.restart spring boot with devtools
As you can see the application was redeployed in just under 2 seconds. I saved about 4 seconds. It doesn’t seem like a lot but you may change your mind doing some math: 4 seconds * 30 restarts a day * 5 days * 4 weeks = 2400 seconds = 40 minutes. And after a month of using it, I just got 40 more minutes for doing stuff other than waiting 🙂

I know that you may still be not satisfied with the savings. I have another project under jacket. This time it’s a fully-fledged microservice with multiple dependencies like Spring Data, Spring Cloud etc.

Without Spring Boot DevTools:restart without devtools

With Spring Boot DevTools:restart with devtools

I’m going to leave the math to you.

Force IntelliJ to auto recompile on file modification

In Eclipse, saving a modified file starts a project rebuild that triggers the application restart operation. In IntelliJ Idea you have to manually rebuild the project. Fortunately, there’s a trick that allows you to recreate the Eclipse’s behaviour.

  1. Go to Preferences and under “Build, Execution, Deployment” check the Build project automatically option. Save & close the window.
  2. Press Ctrl+Shift+A (or ⌘+Shift+A in macOS).
  3. Type “Registry…” in the box and select the first option
  4. Find a key called compiler.automake.allow.when.app.running and tick the value box. You can close the window.

It should look like this: intellij allow automake when app running

Make a change to any class in your project and notice that everytime the IDE triggers rebuild of the project.

Conclusion

And yet one more time Spring Boot makes the development a little more pleasant. Together with an editor that automatically recompiles on file modification we can speed up coding even more. If you want to learn a little more on DevTools, you can visit the official documentation.

2 thoughts on “Automatic Spring Boot project redeploy with DevTools

  1. Konstantin Reply

    As I know it is not enough.
    1) Preferences | Build, Execution, Deployment | Compiler – > Build project automatically

    2) For maven project:
    Preferences | Build, Execution, Deployment | Build Tools | Maven – > Use plugin registry

    3) If Spring Boot project contains multiple maven subprojects then you should as Maven config instead of Spring Boot plugin.

    Best regards

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.