17

I built an application using Spring, JPA, MySQL and Web. I developed a static page in template folder normally and it works.

But, when I change something on static page, I can't reload it with changes. Then, I open the pom.xml and added

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

I restart the application, but, still not working when I make some changes on the static page.

Is there something more to do?

My 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.engsoftware</groupId>
    <artifactId>cobranca</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Cobranca</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
John Mendes
  • 781
  • 3
  • 9
  • 18
  • Try running it from the command line `mvn spring-boot:run` , it's working for me in STS and from command line as well with the same POM. – 11thdimension Mar 31 '17 at 18:43
  • Do you have the [LiveReload extension](http://livereload.com/extensions/) installed? – codingbash Mar 31 '17 at 18:47
  • I have LiveReload extension on Chrome installed. But still not working. If I open in a different browser, show the same page without any changes. I tried clean the cache of browser too, but no success. I clean and install Maven and nothing. – John Mendes Mar 31 '17 at 22:20

9 Answers9

40

In your eclipse top menu your Project -> Build Automatically ON ?

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
user204069
  • 1,215
  • 3
  • 19
  • 25
  • 2
    You might want to add some more detail as to what that setting does and why it would solve the issue. As it stands right now it is not a lot more than a clarification request that should go into a comment. – geisterfurz007 Jun 29 '18 at 20:33
  • Perfect. Thanks – Bandham Manikanta Sep 21 '18 at 07:15
  • How on Earth didn't I think of that before!? It's been bugging me for a couple of days! I tried almost every SpringBoot related setting... didn't occur to me to check if I had accidentally disabled the automatic build in Eclipse - *sigh...* Thanks! – AJPerez Apr 30 '20 at 09:22
  • Thanks a lot. I always forget this. – PowR Jul 17 '20 at 14:00
15

I followed this article https://github.com/spring-projects/spring-boot/issues/7479

So, to devtools works, you must add:

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

The secret is add Optional True and Scope runtime.

John Mendes
  • 781
  • 3
  • 9
  • 18
9

This question is already answered, but for me didn't worked exactly as the accepted answer or other answers tells.

I've got devtools working in the following way:

1) Using the devtools dependency as following:

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

2) Deleting the Spring Maven cache, in Linux it would be:

rm -rf ~/.m2/repository/org/springframework/*

3) Back into Eclipse, pressing Alt+F5 and forcing to clean the project re-downloading every dependency from Maven to your cache.

The key is to set the optional flag to true into the devtools' dependency AND wiping the Maven cache.

Hope this is helpful to someone.

gorcajo
  • 561
  • 1
  • 6
  • 11
4

In addition to the answers given above, you also need to have Build Automatically enabled in Eclipse. It doesn't matter to Dev Tools if your java files are updated, the only thing it looks for is the .class, resource file changes.

For IntelliJ, refer to the diagram below.

enter image description here

Durja
  • 637
  • 13
  • 20
1

According to the Spring Boot docs:

Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes. Note that certain resources such as static assets and view templates do not need to restart the application.

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart

Templates and static assets do not need a restart. Most likely your browser is caching the templates and using the cached version instead of requesting the new template. If you clear the browsers cache, you should see the updated template.


EDIT:

Depending on your template technology you're using, you will need to set a property in your properties file to disable the template cache

# Thymeleaf
spring.thymeleaf.cache = false

#FreeMarker
spring.freemarker.cache = false

#Groovy
spring.groovy.template.cache = false

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html#howto-reload-static-content

Jason White
  • 5,495
  • 1
  • 21
  • 30
  • I have LiveReload extension on Chrome installed. But still not working. If I open in a different browser, show the same page without any changes. I tried clean the cache of browser too, but no success. I clean and install Maven and nothing. – John Mendes Mar 31 '17 at 22:21
  • @JohnMendes In Eclipse, have you tried a clean and rebuild of the project after you added the devtools dependency? Also what are you using for templates? – Jason White Mar 31 '17 at 22:36
  • Just looked at your pom and saw the dependency for Thymeleaf. You'll need to set the Thymeleaf template cache to false in you properties file. spring.thymeleaf.cache=false – Jason White Mar 31 '17 at 22:43
0

Even after trying above mentioned answers, if it is still not working just try enabling build automatically for the project in eclipse. It worked for me after that.

ABC
  • 45
  • 2
  • 8
0

This worked for me. Add the below line in application.properties file,

spring.devtools.restart.enabled=true
0

For my case the problem is on Eclipse's Java Build Path (Right Click Project > Properties > Java Build Path > Source Tab), end up there is an error on one of the source folder which doesn't exist at all. After I remove that folder from build path, it started auto build.

Newbie
  • 1,584
  • 9
  • 33
  • 72
0

The other reason for someone with the same issue is because under the PARENT tag for Spring-Boot-Starter-Parent Artifact Id, the version is already mentioned, so incase you have copied the dependency from any other source that the Spring Starters Project, you don't need to mention the version explicitly in the Spring DevTools Dependency. So just remove the Version & Version Tag from it.

enter image description here