Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring Boot 1.3, spring-boot-devtools and Thymeleaf templates won't do live reload when changed in Netbeans

Spring Boot 1.3 introduced spring-boot-devtools to provide similar functionality as Spring Reloaded to reload modified classes and to update Thymeleaf templates without having to re-run your application.

I have been using Spring Boot 1.2.7 (with Spring Reloaded) before and I was able to modify my templates on the fly without having to restart my Spring Boot application.

Same application is now neither reloading the Thymeleaf templates nor reloading/restarting the application when I modify and save Java code / Thymeleaf templates.

I am using Netbeans 8.0.2 and Maven (version 3.0.5) found embedded in the Netbeans IDE. The application is packaged as JAR.

In Netbeans, under the project Properties -> Build -> Compile is a checkbox "Compile On Save" which is ticked. I verified that this actually works by modifying .java file and checking the timestamps in the /target/classes.

Here are the "Run action" properties for the project in Netbeans:

I have the following depedencies in my pom.xml (including others, excluded for not being relevant):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency> 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

With this, I should be set to go, as Spring Boot Blog mentions the following:

"When you have the spring-boot-devtools module included, any classpath file changes will automatically trigger an application restart."

and similar remarks are made in the Spring Boot official documentation.

Edit: I tried to use spring-boot-maven-plugin with version tag 1.2.7.RELEASE and suddently changes to my Thymeleaf templates are visible in browser when the template is saved. It seems that at least the problem with Thymeleaf templates is not because of spring-boot-devtools, but rather because of the spring-bot-maven-plugin.

The problem can be divided into two parts:

1) Thymeleaf templates which won't reload for some reason if newer version of spring-boot-maven-plugin is used (1.3.0.RELEASE) 2) Application reload/restart trigger won't happen, even though .class files in /target/classes get updated when the respective .java files are modified and saved.

Update: Verified that devtools aren't loaded (Main thread name isn't restartedMain). Solved 2) by changing Execute goals in Run project Action in Netbeans project properties to the following:

process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec

Old Execute goals was package spring-boot:run. Googling a bit revealed others having problem with spring-boot-devtools when the project is run with spring-boot:run.

Now the only problem is that the Thymeleaf templates don't get updated live when saved.

like image 734
Laurenzo Avatar asked Mar 14 '23 02:03

Laurenzo


2 Answers

Change Execute goals in Run project Action in Netbeans project properties to the following:

process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec instead of package spring-boot:run enables Spring Boot Devtools and restart works as expected.

Problem with Thymeleaf templates was attributed to the fact that in Spring Boot 1.3, The Spring Boot Maven plugin no longer adds src/main/resources directly to the classpath. See release notes for details.

Configuring explicit resource directory location (in my case src/main/resources) to pom.xml resolves the problem with Thymeleaf templates not reloading:

<build>
   ...
   <resources>
     <resource>
       <directory> src/main/resources </directory>
     </resource>
   </resources>
  ... 
 </build>
like image 199
Laurenzo Avatar answered Mar 16 '23 19:03

Laurenzo


I use Spring Booth 1.4.2.RELEASE. And LiveReload works for me after I did the following:

  • Install LiveReload extension of Chrome
  • Add addResources to the spring-boot-maven-plugin maven configuration.

File pom.xml

<build>
    ...
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    ...
    </plugins>
    ...
</build>

Reference: http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html

like image 35
James J. Ye Avatar answered Mar 16 '23 19:03

James J. Ye