【问题标题】:Using Spring Boot 1.3, spring-boot-devtools and Thymeleaf templates won't do live reload when changed in Netbeans使用 Spring Boot 1.3,在 Netbeans 中更改时 spring-boot-devtools 和 Thymeleaf 模板不会进行实时重新加载
【发布时间】:2015-11-23 10:54:10
【问题描述】:

Spring Boot 1.3 引入了 spring-boot-devtools 以提供与 Spring Reloaded 类似的功能,以重新加载修改后的类并更新 Thymeleaf 模板,而无需重新运行您的应用程序。

我之前一直在使用 Spring Boot 1.2.7(重新加载了 Spring),我能够即时修改我的模板,而无需重新启动我的 Spring Boot 应用程序。

当我修改和保存 Java 代码/Thymeleaf 模板时,同一个应用程序现在既不会重新加载 Thymeleaf 模板,也不会重新加载/重新启动应用程序。

我正在使用 Netbeans IDE 中嵌入的 Netbeans 8.0.2 和 Maven(版本 3.0.5)。应用程序被打包为 JAR。

在 Netbeans 中,在项目 Properties -> Build -> Compile 下是一个复选框“Compile On Save”,它被选中。 我通过修改 .java 文件并检查 /target/classes 中的时间戳来验证这确实有效。

这里是 "Run action" properties for the project in Netbeans:

我的 pom.xml 中有以下依赖项(包括其他,因不相关而被排除):

<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>

有了这个,我应该准备好了,因为 Spring Boot 博客提到以下内容:

“当您包含 spring-boot-devtools 模块时,任何类路径文件更改都会自动触发应用程序重启。”

类似的言论在Spring Boot official documentation.

编辑: 我尝试使用带有版本标签 1.2.7.RELEASE 的 spring-boot-maven-plugin 并且在保存模板时突然更改我的 Thymeleaf 模板在浏览器中可见。看来至少 Thymeleaf 模板的问题不是因为 spring-boot-devtools,而是因为 spring-bot-maven-plugin。

问题可以分为两部分:

1) 如果使用较新版本的 spring-boot-maven-plugin (1.3.0.RELEASE),Thymeleaf 模板由于某种原因不会重新加载 2) 应用程序重新加载/重启触发器不会发生,即使 /target/classes 中的 .class 文件在相应的 .java 文件被修改和保存时得到更新。

更新:验证没有加载开发工具(主线程名称未重新启动Main)。 解决 2) 通过将 Netbeans 项目属性中的 Run project Action 中的 Execute 目标更改为以下内容:

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

旧的执行目标是package spring-boot:run。谷歌搜索了一下,当项目使用 spring-boot:run 运行时,其他人发现 spring-boot-devtools 存在问题。

现在唯一的问题是 Thymeleaf 模板在保存时不会实时更新。

【问题讨论】:

  • "任何类路径文件更改"
  • @StéphaneNic​​oll:Netbeans 正在编译保存 Java 源代码时需要更新的 .class 文件。由于您提示检查主线程名称,我能够验证 devtools 没有运行 - 谢谢!
  • 那你能回答你自己的问题吗?谢谢。
  • Thymeleaf 模板没有被“实时更新”仍然存在问题。

标签: spring-boot thymeleaf netbeans-8


【解决方案1】:

将 Netbeans 项目属性中运行项目操作中的执行目标更改为以下内容:

process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 而不是 package spring-boot:run 启用 Spring Boot Devtools 并按预期重新启动。

Thymeleaf 模板的问题归因于在 Spring Boot 1.3 中,Spring Boot Maven 插件不再将 src/main/resources 直接添加到类路径中。 See release notes for details.

为 pom.xml 配置显式资源目录位置(在我的情况下为 src/main/resources)解决了 Thymeleaf 模板未重新加载的问题:

<build>
   ...
   <resources>
     <resource>
       <directory> src/main/resources </directory>
     </resource>
   </resources>
  ... 
 </build>

【讨论】:

    【解决方案2】:

    我使用 Spring Booth 1.4.2.RELEASE。在我执行以下操作后,LiveReload 对我有用:

    • 安装LiveReload Chrome 扩展
    • addResources 添加到spring-boot-maven-plugin maven 配置中。

    文件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>
    

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

    【讨论】:

    • 谢谢! Spring boot 1.5.4 也明确需要这个配置。
    猜你喜欢
    • 2016-05-05
    • 2016-04-11
    • 2018-08-29
    • 1970-01-01
    • 2020-03-14
    • 2016-01-26
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    相关资源
    最近更新 更多