Live reload not working in Spring boot devtools
Asked Answered
H

3

9

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?

Hydrosphere answered 20/3, 2018 at 14:6 Comment(0)
R
16

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

Rattoon answered 20/3, 2018 at 16:9 Comment(2)
Good answer. It's also important to note that you also have to "build project" (not maven install) in the IDE to trigger a restart.Stung
do you mean use the mvn install command on maven? and gradle build command on gradle, in second terminal or command prompt? It is very complicated if you doing everything without an IDE, which is not building your application on save. you probably setup a gulp task using nodejs to watch files and build on changes.Tsingyuan
N
2

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.

Naarah answered 10/9, 2021 at 16:12 Comment(0)
C
0

Set Build project automatically. In intellij  File –> Setting –> Build, Execution, Deployment –> Compiler –> check the Build project automatically In eclipse  Window -> Preferences -> Workspace -> General -> Build -> Check ‘build automatically’

Curtal answered 26/11, 2022 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.