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:
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.
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:
With Spring Boot 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.
- Go to Preferences and under “Build, Execution, Deployment” check the Build project automatically option. Save & close the window.
- Press Ctrl+Shift+A (or ⌘+Shift+A in macOS).
- Type “Registry…” in the box and select the first option
- 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:
Make a change to any class in your project and notice that everytime the IDE triggers rebuild of the project.
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.