Spring Boot Automatic Restart using Developer Tools with Maven

By Arvind Rai, December 07, 2023
When our application uses Spring Boot developer tools then it will automatically restart the server and refresh the page on browser whenever a file within classpath is modified in development mode. We run our exploded form of the project using Spring Boot maven command mvn spring-boot:run . Spring Boot developer tool is spring-boot-devtools that needs to be configured in maven or gradle. Developer tools are used for fast development. When we work with an IDE with exploded form of the project then after modifying any file we need to test our application. To save the development time there are many plugins such as JRebel that can reload the file changes using classloader. Spring Boot also provides a tool to help developer for fast development that is spring-boot-devtools. Here on this page we will understand one of the features of developer tools to automatic restart the server as well as page refresh on browser for any change in file within classpath. To get page refresh we need to install LiveReload in the browser. Developer tools provide many properties that will be configured in application properties file to control the automatic restart. Now we will provide the example step by step to use developer tools for automatic restart.

1. Configure Developer Tools

Spring provides spring-boot-devtools for developer tools. These tools are helpful in application development mode. One of the features of developer tool is automatic restart of the server. To configure developer tools using maven we need to add spring-boot-devtools dependency as follows.
If we are using Gradle, we will add spring-boot-devtools dependency as follows.
When using developer tools we should know the following points.
1. When we create a JAR or WAR as fully packaged application and run it then developer tools are automatically disabled.
2. When we run the application using java -jar or special classloader, then it is considered a "production application" and developer tools will be automatically disabled.
3. It is best practice to set <optional>true</optional> that will avoid developer tools to apply transitively on other module. Gradle does not support this feature for the now.
4. We can also use excludeDevtools in maven and gradle both to ensure that developer tools JAR is never included in production build.
5. When developer tools has been configured, the project in exploded form are started using restart classloader and fully packaged application are started using base classloader by Spring Boot.

2. Install LiveReload in Browser

In SpringMVC project, there is involvement of a web server and browser. In development mode whenever we change any file, generally we need to restart the server and refresh the browser to get updated data. Spring Boot developer tools have automatized these two tasks. To refresh browser automatically we need to install LiveReload. In our example I am using Firefox. I need to install LiveReload in my Firefox. Follow the below steps to get ready with LiveReload.
1. Go to the LiveReload extension link and install it.
2. To work with LiveReload, we need to run LiveReload server. For us Spring Boot developer tool will start a LiveReload server.
3. LiveReload can be enabled and disabled. To refresh the page using LiveReload, it must be enabled.
4. In my example, I am using LiveReload with Firefox. To enabled it, first go to Firefox customize view and from there, add LiveReload to toolbar. Now right click on LiveReload icon and enable it. Before enabling it, make sure that maven mvn spring-boot:run command is running that will start tomcat and LiveReload server.

3. Automatic Restart

Spring Boot developer tools have a feature that will auto restart the server and refresh the page on browser whenever a file on classpath is modified. Once we have configured developer tools in our maven or gradle then in eclipse after changing file, when we save, the auto restart is fired. All files that are in classpath, does not need server restart. Many files are there that only needs page refresh on browser for any change in the file. When we perform any change in Java file and save it then server restart and page refresh on browser both required. Some files does not require server restart but require page refresh on browser only, for example static pages, Thymeleaf templates , HTML pages etc.
Spring Boot developer tools performs only page refresh on browser by default for following paths on classpath that also requires LiveReload installed on browser. These directory patterns are as follows.

Now we will create a sample application with Spring MVC and Thymeleaf. Here we will also create some files that will not be in classpath. Find the project structure.
Spring Boot Automatic Restart using Developer Tools with Maven
The files located in classpath are as follows.
1. Java files: Automatic Server Restart and page refresh on Browser both
2. Resources files: By default automatic page refresh on Browser only
We have also created some files that are not in classpath.
Now find our maven file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<description>Spring Boot Automatic Restart</description>

Find the steps to test automatic restart.
1. Download my demo project source code using the link and import it into eclipse.
2. Go to root directory of the project using command prompt and run the command mvn clean eclipse:eclipse and refresh the project folder in eclipse. This creates a classpath file.
3. Now run command mvn spring-boot:run that will start a tomcat server. Or go to the main class in IDE. Right click and Run As -> Java Application
4. Now go to the browser and access the URL http://localhost:8080/ or http://localhost:8080/hello
5. Click on the LiveReload icon on browser toolbar menu to enable automatic page refresh.
6. Now change something in java files and save it. We will observe that server will restart and page on browser will refresh.
7. Now change something in HTML files. We will observe that only page will refresh on the browser and server will not restart.

4. Watching Additional Paths

Till now we have understood that Spring Boot developer tools watch only those files which are lying under classpath for automatic restart. Now we will include those files which are not in classpath. In our project we have created a directory named as files outside of the classpath that contains following files.


Though files directory is not lying in the classpath, it still can take the advantage of automatic restart. To achieve it, Spring Boot provides property that needs to configure in application properties file as follows.
spring.devtools.restart.additional-paths = files 
Here files is our directory which I have created within root directory of the project. After configuring the property, we need to re-run the command mvn spring-boot:run and now test automatic restart by changing something in files apptrigger.txt or txtfile.txt and save it, we will observe that server will restart and page will refresh on browser.

5. Using a Trigger File

Spring Boot provides a property that configures a trigger file to achieve that if trigger file is modified only when developer tools will start watching for file change. In development mode using an IDE, we frequently change the files and save it. To avoid unnecessary automatic restart, we can use a trigger file. Whenever we modify trigger file only when developer tools should watch for any change in files and if there is any change in files, automatic restart should fire. Trigger file can be modified manually or we can use IDE plugin. To configure trigger file, Spring Boot provides spring.devtools.restart.trigger-file property that can be configured as follows.
spring.devtools.restart.additional-paths = files
spring.devtools.restart.trigger-file = apptrigger.txt 
In the above configuration apptrigger.txt is our trigger file which is located on the path files\apptrigger.txt. Find the below steps to test trigger file.
1. First re-run the command mvn spring-boot:run
2. Now modify any file, suppose I am modifying any java file for example HelloController.java and then modify trigger file i.e apptrigger.txt. We will observe that automatic restart will fire.
3. We need to understand that if we modify only apptrigger.txt, then there will be no automatic restart. This is because when we modify trigger file apptrigger.txt, then developer tools start to watch for any change in files and because we have modified no other files, so automatic restart will not happen.

6. Excluding Resources

The files which lie under the classpath, Spring Boot developer tools watch for full auto restart but template and static files with folder structure /META-INF/maven, /META-INF/resources ,/resources ,/static ,/public or /templates, will get only page refresh by default. We can break this default setting for template and static files and can achieve full auto restart. To achieve it we need to configure spring.devtools.restart.exclude property as follows.
spring.devtools.restart.exclude = static/** 
Let us understand what will happen now.
1. First re-run command mvn spring-boot:run
2. Now if we modify the file templates\hello.html, developer tool will auto restart the server and as well as refresh the page.
3. If we modify the file static\index.html, developer tool will only refresh the page.
4. If we want to exclude more folders from full auto restart, we can configure comma separated as follows.

7. Disabling Restart

If we want to disable auto restart feature from developer tools, we can achieve it by configuring the property spring.devtools.restart.enabled in two ways.
1. Using application property file
spring.devtools.restart.enabled = false 
In this case developer tools will initialize the restart classloader but will not watch for file changes.

2. Using system property before calling SpringApplication.run() as follows.
public class Application {
    public static void main(String[] args) {
    	System.setProperty("spring.devtools.restart.enabled", "false");  
        SpringApplication.run(Application.class, args);
In this case developer tools will not initialize the restart classloader and also it will not watch for file changes. Hence auto restart is completely disabled.

8. Useful Developer Tools Properties for Automatic Restart

Here we are listing useful developer tools properties for automatic restart. The properties are configured in application.properties file.
spring.devtools.restart.additional-paths: Add paths for auto restart that are not in classpath.
spring.devtools.restart.trigger-file: Specify a trigger file name and now developer tools watch for restart check only when if trigger file is modified.
spring.devtools.restart.exclude: Specify the path to exclude the files from full auto restart.
spring.devtools.restart.additional-exclude: Specify the additional path that will be excluded from full auto restart keeping the default settings.
spring.devtools.restart.enabled: If the value is false then developer tools will stop watching the file for auto restart.

Now we will provide the properties that are used in customizing restart loader and that will be configured in META-INF/spring-devtools.properties file.
restart.include. : It is a prefix using which we declare a property and assign the JAR name with regex pattern to include a JAR from base classloader to restart classloader.
restart.exclude. : It is a prefix using which we declare a property and assign the JAR name with regex pattern to exclude a JAR from restart classloader to base classloader.

9. Reference

Using Spring Boot: Developer tools

10. Download Source Code


©2024 concretepage.com | Privacy Policy | Contact Us