Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live reload not working in Spring boot devtools

I am using this dependency in a spring boot application:

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

The documentation says:

By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload.

The live reload documentation says:

The spring-boot-devtools module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed. LiveReload browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.

Now, I am using Maven and my static folder is under src/main/resources, so my folder structure is:

src/main/resources/static/index.html

This is what's in my index.html file:

<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>HI THERE! Get your greeting <a href="/greeting">here</a></p>
</body>
</html>

I using chrome browser but am not using any live reload browser extension in chrome.

I run the application with this Powershell command (since I'm using spring boot maven plugin):

mvn clean package; java -jar target\project-name-version.jar

This starts up the server on localhost:8080 and displays the contents of index.html in a web page.

However, when I then make changes to index.html in Eclipse IDE and save the file, and I then refresh the browser page, I do not see the new changes.

How come live reload isn't working for me? What am I doing wrong?

like image 493
Kingamere Avatar asked Mar 20 '18 14:03

Kingamere


2 Answers

Running the application from the IDE is not a constraint to make Developer tools working. Your problem is somewhere else.

These commands :

mvn clean package; 
java -jar target\project-name-version.jar

mean that you don't use the spring-boot maven plugin to run your application.
You run the autobootable jar of your fully packaged application. Consequently, Spring Boot devtools are disabled as stated by the documentation :

Developer tools are automatically disabled when running a fully packaged application. If your application is launched from java -jar or if it is started from a special classloader, then it is considered a “production application”. Flagging the dependency as optional is a best practice that prevents devtools from being transitively applied to other modules that use your project. Gradle does not support optional dependencies out-of-the-box, so you may want to have a look at the propdeps-plugin.

To run the application from command line in exploded/dev mode, mvn clean package is not required and helpless.
Just execute mvn spring-boot:run

like image 188
davidxxx Avatar answered Oct 03 '22 17:10

davidxxx


I fixed this issue by below approach -

  • First open setting.json (ctrl + shift+P) file and define a property there as "java.autobuild.enabled": true.
  • second open application.properties file and define a property as spring.devtools.restart.pollInterval=10s

Hope it should work.

like image 40
Abhishek Kumar Avatar answered Oct 03 '22 18:10

Abhishek Kumar