Spring Boot Automatic Restart using Developer Tools with Maven

Software Used

spring-boot-devtools

spring-boot-devtools

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>

spring-boot-devtools

compile("org.springframework.boot:spring-boot-devtools")

Install LiveReload in Browser

Automatic Restart

src\main\java\com\concretepage\Application.java src\main\java\com\concretepage\controller\HelloController.java

src\main\resources\static\index.html src\main\resources\templates\hello.html src\main\resources\application.properties

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

<?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>

Watching Additional Paths

spring.devtools.restart.additional-paths = files

Using a Trigger File

spring.devtools.restart.additional-paths = files spring.devtools.restart.trigger-file = apptrigger.txt

HelloController.java

apptrigger.txt

apptrigger.txt

apptrigger.txt

Excluding Resources

spring.devtools.restart.exclude = static/**

spring.devtools.restart.exclude=static/**,templates/**

Disabling Restart

spring.devtools.restart.enabled = false

SpringApplication.run()

@SpringBootApplication public class Application { public static void main(String[] args) { System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(Application.class, args); } }

Useful Developer Tools Properties for Automatic Restart

application.properties

META-INF/spring-devtools.properties

Reference

Download Source Code

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. Spring boot developer tool isthat 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. 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.We are using following software in our application.1. Java 82. Maven 3.3.93. Spring Boot 1.4.3.RELEASE4. Eclipse Mars Spring providesfor 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 adddependency as follows.If we are using Gradle, we will adddependency 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 usingor special classloader, then it is considered a "production application" and developer tools will be automatically disabled.3. It is best practice to setthat will avoid developer tools to apply transitively on other module. Gradle does not support this feature for the now.4. We can also usein 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 usingclassloader and fully packaged application are started usingclassloader by spring boot.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 mavencommand is running that will start tomcat and LiveReload server.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/templatesNow 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.The files located in classpath are as follows.We have also created some files that are not in classpath.Now find our maven file.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 commandand refresh the project folder in eclipse. This creates a classpath file.3. Now run commandthat will start a tomcat server. Or go to the main class in IDE. Right click and4. Now go to the browser and access the URLor5. 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.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 asoutside of the classpath that contains following files.files\apptrigger.txtfiles\txt\txtfile.txtThoughdirectory 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.Hereis our directory which I have created within root directory of the project. After configuring the property, we need to re-run the commandand 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.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 providesproperty that can be configured as follows.In the above configurationis our trigger file which is located on the path. Find the below steps to test trigger file.1. First re-run the command2. Now modify any file, suppose I am modifying any java file for exampleand then modify trigger file i.e. We will observe that automatic restart will fire.3. We need to understand that if we modify only, then there will be no automatic restart. This is because when we modify trigger file, 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.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 configureproperty as follows.Let us understand what will happen now.1. First re-run command2. Now if we modify the file, developer tool will auto restart the server and as well as refresh the page.3. If we modify the file, 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.If we want to disable auto restart feature from developer tools, we can achieve it by configuring the propertyin two ways.1. Using application property fileIn this case developer tools will initialize the restart classloader but will not watch for file changes.2. Using system property before callingas follows.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.Here we are listing useful developer tools properties for automatic restart. The properties are configured infile.: Add paths for auto restart that are not in classpath.: Specify a trigger file name and now developer tools watch for restart check only when if trigger file is modified.: Specify the path to exclude the files from full auto restart.: Specify the additional path that will be excluded from full auto restart keeping the default settings.: If the value isthen 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 infile.: It is a prefix using which we declare a property and assign the JAR name with regex pattern to include a JAR fromclassloader toclassloader.: It is a prefix using which we declare a property and assign the JAR name with regex pattern to exclude a JAR fromclassloader toclassloader.