Home  >  Spring Boot

Spring Boot Automatic Restart using Developer Tools with Maven

By Arvind Rai, January 15, 2017
This page will walk through Spring boot automatic restart using developer tools with maven. 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.

Software Used

We are using following software in our application.
1. Java 8
2. Maven 3.3.9
3. Spring Boot 1.4.3.RELEASE
4. Eclipse Mars

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.
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
</dependency> 
If we are using Gradle, we will add spring-boot-devtools dependency as follows.
compile("org.springframework.boot:spring-boot-devtools") 
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.

Install LiveReload in Browser

In spring MVC 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.

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.
/META-INF/maven
/META-INF/resources
/resources
/static
/public
/templates

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
src\main\java\com\concretepage\Application.java
src\main\java\com\concretepage\controller\HelloController.java 
2. Resources files: By default automatic page refresh on Browser only
src\main\resources\static\index.html
src\main\resources\templates\hello.html
src\main\resources\application.properties 
We have also created some files that are not in classpath.
files\apptrigger.txt
files\txt\txtfile.txt 
Now find our maven file.
pom.xml
<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.concretepage</groupId>
	<artifactId>spring-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<description>Spring Boot Automatic Restart</description>

	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.4.3.RELEASE</version>
	    <relativePath/>
	</parent>
	<properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	    <java.version>1.8</java.version>
	</properties>
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
	</dependencies> 
	<build>
	    <plugins>
		<plugin>
		   <groupId>org.springframework.boot</groupId>
		   <artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	    </plugins>
	</build>
</project> 
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.

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.

files\apptrigger.txt
files\txt\txtfile.txt

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.
application.properties
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.

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.
application.properties
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.

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.
application.properties
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.
spring.devtools.restart.exclude=static/**,templates/** 

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
application.properties
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.
@SpringBootApplication
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.

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.

Reference

Using Spring Boot: Developer tools

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS



Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us