Tired of rebuilding your Spring Boot web application every time you change your HTML markup or Java class? Thanks to IntelliJ IDEA and SpringBoot DevTools, you can easily code and debug faster without wasting time rebuilding your application over and over again. The solution discussed here would run your project as soon as your code change, and it would refresh your Thymeleaf HTML templates for the front end without even having to rebuild the project.
The actual work required to configure your project is not much. All, in all, you need to add a dependency, set up a couple of configurations and make sure that your IDE updates the CLASSPATH, every time you make a code change.
Triggering a restart
As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart. In IntelliJ IDEA, building the project (Build -> Build Project) has the same effect.
– https://docs.spring.io
First thing we need to do is to add “spring-boot-devtools” maven dependency, to your pom.xml:
<!--Spring Boot Dev Tools--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
Next, add following lines to src/main/resources/application.properties file:
spring.thymeleaf.cache=false spring.devtools.restart.enabled=true spring.devtools.livereload.enabled=true
And you are done … but, you may want to lay the groundwork for using this solution in real-world for debug and release environments.
We need to start by creating environment specific application.properties file. Let’s start by replacing the 3 lines we added to application.properties file above with following:
# change this value from "debug" to "release" before release spring.profiles.active=debug
Next, create application-debug.properties and application-release.properties files, under resources directory, with following content. The idea is to use this auto-refresh mechanism only for your development, not for your releases.
spring.thymeleaf.cache=false spring.devtools.restart.enabled=true spring.devtools.livereload.enabled=true
If you have used Node.js earlier, this is the devDependencies, equivelent in your package.json
Finally, let’s delete the “spring-boot-devtools” maven dependency from pom.xml we added earlier, and let’s create two profiles for your maven build as following:
... </plugin> </plugins> </build> <profiles> <profile> <id>debug</id> <activation> <activeByDefault>true</activeByDefault> </activation> <dependencies> <!--Spring Boot Dev Tools--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> </profile> <profile> <id>release</id> <dependencies> </dependencies> </profile> </profiles> </project>
Notice, that I am setting the property as profile.activation.activeByDefault=true, for debug. That way, this dependency stays only for debugging.
cheers!