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

你说的曾经没有我的故事 提交于 2019-12-10 19:12:38

问题


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.


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/33869285/using-spring-boot-1-3-spring-boot-devtools-and-thymeleaf-templates-wont-do-liv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!