使用 Spring Boot 1.3,spring-boot-devtools 和 Thymeleaf 模板在 Netbeans 中更改时不会执行实时重新加载



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

我以前一直在使用Spring Boot 1.2.7(带有Spring Reloaded),并且我能够在不重新启动Spring Boot应用程序的情况下动态修改我的模板。

当我修改和保存Java代码/Thymeleaf模板时,相同的应用程序现在既没有重新加载Thymelaf模板,也没有重新加载/重新启动应用程序。

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

在Netbeans中,项目Properties(属性)->Build(构建)->Compile(编译)下有一个复选框"Compile On Save(保存时编译)",它被勾选。我通过修改.java文件并检查/target/classes中的时间戳来验证这一点。

这是Netbeans中项目的"运行操作"属性:

我在pom.xml中有以下depedeties(包括其他depedeties,因不相关而排除在外):

<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官方文档中也有类似的说明。

编辑:我尝试使用版本标记为1.2.7.RELEASE的spring-boot-maven插件,当保存模板时,浏览器中可以看到对Thymelaf模板的突然更改。看来Thymelaf模板的问题至少不是因为spring-boot开发工具,而是因为spring-bot maven插件。

问题可以分为两部分:

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

更新:已验证没有加载devtools(主线程名称没有restartedMain)。解决2)通过将Netbeans项目属性中Run项目Action中的Execute目标更改为以下内容:

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

旧的执行目标是package spring-boot:run。谷歌搜索显示,当项目使用spring-boot:run运行时,其他人对spring-boot开发工具有问题。

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

将Netbeans项目属性中Run project Action中的Execute目标更改为以下内容:

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

Thymelaf模板的问题归因于以下事实:在Spring Boot 1.3中,Spring Boot Maven插件不再将src/main/resources直接添加到类路径中。有关详细信息,请参阅发行说明。

将显式资源目录位置(在我的例子中是src/main/resources)配置到pom.xml解决了Thymelaf模板不重新加载的问题:

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

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

  • 安装Chrome的LiveReload扩展
  • 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

最新更新