Spring Boot Devtools
Spring boot devtools has become a very popular dependency for rapid development and here’s why: Every developer wants to be productive and in many cases the speed with which the developer is able to move forward while adding features or fixing bugs is directly proportional to the frequency of the “edit-compile-run” cycle. Disciplines to create great code encourage developers to have really short cycle times in order of seconds.
Whether it’s developing RESTful API’s or web applications in a typical Java environment such as Eclipse or STS, each such cycle requires restarting the server, in order to make the server to pick up the changes. This is a cumbersome & wasteful process as restarting the server not only takes a lot of time depending on the size of the application but also requires developers to switch context making them less productive. You might have heard of the JRebel plugin which is a solution to this problem, but JRebel is not free depending on your use. What if there was a free tool to accomplish server restart and live-reload in a reliable and fast way ? This is where spring boot devtools comes in. By adding this to any Java Spring project, the developer can speed up the edit-compile-run cycle – the compile part is already addressed by STS or Eclipse and the run part is where devtools package helps.
Adding Spring Boot Devtools
To enable spring boot devtools package add it to your project’s dependencies. In Maven, for example, just add the spring-boot-devtools dependency to your pom.xml.
After saving pom.xml, maven will download the necessary dependencies in the background and include it in your project in the next compilation. If you are using STS, you can see these steps happening in the “Progress” tab.
There is an automatic restart of the applications which are using devtools, whenever a change occurs in the files of the classpath. Any entry on the classpath which points to a particular folder is monitored for changes, by default. The power of this feature is realised when there is an instant feedback for any change in the code, while working in any IDE like Eclipse or STS.
The Auto-restart feature automatically restarts the server by configuring and reloading the code in the server side, when there is a change in the source code. Thus, this feature eliminates the time to check if the changes are picked up by the server and thus enhances the speed of the development process. You can see auto-restart in action below.
Some folders like /templates, /static, /public, /resources, /META–INF/maven, /META-INF/resources do not necessarily require a restart, when a change occurs. These can be customized by using spring.devtools.restart.exclude property.
For example, if you want to exclude the
/static folder from monitoring for auto-reload, you can configure this exclusion, by setting this property in the
application.properties file. For example,
With this setting, any change to files under
/static will not trigger a restart, but will still trigger a live-reload. More on live-reload below.
Watch additional paths
It is also possible to restart when changes are made to files which are outside the classpath. This is achieved by setting the
spring.devtools.restart.additional-paths property in
Apart from the above two, spring boot devtools provides one more property that would help trigger auto-reload on a single file. By setting
spring.devtools.restart.trigger-file property to the path of the file that needs to trigger the restart, the server will auto restart every time this file changes on disk and will not restart when the classpath items change. For example,
This provides for very fine-grained control to prevent too many restarts triggered by IDE auto-compliation during a typical edit cycle involving multiple files and the developer can trigger restart by modifying & saving this file. This triggers the restart check – a restart happens only if during this check spring boot devtools detects if something has changed.
Global devtools settings
Apart from customizing at project level by editing application.properties global devtools settings can be set in a file named
.spring-boot-devtools.properties in the home directory of the user. Of course, the project-specific settings override global settings and global setting apply to all projects worked on by the user. This allows, for example, a developer to edit multiple projects and trigger a reload simultaneously after performing a consistent set of edits. By setting
~/..spring-boot-devtools.properties the developer can run, e.g.,
touch ~/.spring-boot-devtools.properties anytime she wishes to trigger an auto-reload check. Again, only those projects where changes are detected will be restarted.
Even if the application running on the server is auto-restarted, the developer would need to refresh the browser by manually clicking the refresh button when something changes while developing a web application. To circumvent this, Spring boot devtools provides integration to LiveReload by including an in-built embedded LiveReload Server, which automatically triggers the browser to refresh when a change is detected. This can be done by installing the LiveReload plugin on the browser. By installing this extension to the browser, the web-page gets automatically refreshed when there is a server restart or a change in the resources. These LiveReload browser extensions for Firefox, Chrome and Safari are available for free from livereload.com.
Note: Only one LiveReload server can run at a time.
Debugging Remote application with Spring Boot Devtools
The steps explained above are useful to debug a locally running application. But the same method can be extended to debug remote applications also. Spring Boot tunnels the debug data over HTTP and enables the developer to debug a JVM running remotely. To set this up, 3 additional steps are required:
- Ensure devtools is included in the repackaged archive. Check the following configuration is inclided in your pom.xml. Pay attention to the <configuration> tag.
- Add secret to authenticate the connection to the remote application.
spring.devtools.remote.secret=<YOUR SECRET HERE>
This is set once is application.properties and used both while building the application to be deployed remotely as well as by the debugger running locally when it tries to connect remotely.
- Deploy the application for the first time to remote URL. Repeat this step any time the value of secret is modified. Ensure the remote JVM is running in debug mode. The exact method to do this will depend on the remote deployment method. For example, if you have a dockerized web-app running on a remote server to which you are deploying, edit the dockerfile to launch
java -Xdebug ...and rebuild the docker image if necessary.
- Add a new Run Configuration to your project with the same classpath as your project. Set the main class to
org.springframework.boot.devtools.RemoteSpringApplicationand provide the URL from step 3 as the argument. See docs for more details.
The application can then be debugged remotely using the newly added Run Configuration. See this video for an example with a web-app deployed in cloud-foundry.
With this setup Live Reload also works in the same method as explained above. Although the application is running remotely the LiveReload server runs locally and triggers the browser to refresh after the change has been pushed successfully.
Below is an example
application.properties file that demostrates spring boot devtools configuration settings.
# Uncomment below line to disable auto-restart
# spring.devtools.restart.enabled = false
Trouble-shooting Spring Boot Devtools
Spring boot dev tools recommended environment set-up.
Make sure you have Spring boot 1.3 or later to use the spring-boot-devtools dependency.
Java 9 is not recommended for using spring boot. Java7/8 is preferred.
If you are using Eclipse, make sure you have the right version of Eclipse installed. Recommended Eclipse Version
Eclipse Java EE version
Use case 1: Spring boot devtools not working in Eclipse
Check for the following:
Ensure that the
spring-boot-devtoolsdependency is added to your pom.xml:Java123456<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>runtime</scope></dependency>
After adding the devtools dependency, clean and rebuild the project and also enable the build automatically. In Eclipse, in the top set the Project → “Build Automatically” to ON.
Check if LiveReload plugin is installed in the browser which you are using.
- Clear the browser’s cache. This is done to make sure that your browser is requesting the new HTTP resources instead of using the cached vesion (especially those resources, which, by default do not need auto restart).
Check if you have set the cache property of the template you are using to false, in order to disable the template’s cache. For example, to disable the cache of ‘thymeleaf’ template set the property as follow:
spring.thymeleaf.cache = false
Use case 2: Spring boot Live Reload is not working in Intellij IDEA
Troubleshoot by following the below steps,
Ensure that your pom.xml includes the ‘spring -boot-devtools’ dependency.
Navigate to Settings → Build, Execution, Deployment → Compiler in your IntelliJ IDE and check the option ‘Make project automatically’
Open the IntelliJ’s registry key by pressing SHIFT+CTRL+A. Type ‘registry’ on the window which is popped up and cheek the option ‘compiler.automake.allow.when.app.running’ by checking the check box.
Restart your application to check if LiveReload is working in IntelliJ IDE.