使用Spring Boot 1.3,当在Netbeans中进行更改时,spring-boot-devtools和Thymeleaf模板将不会实时重新加载 [英] Using Spring Boot 1.3, spring-boot-devtools and Thymeleaf templates won't do live reload when changed in Netbeans

查看:331
本文介绍了使用Spring Boot 1.3,当在Netbeans中进行更改时,spring-boot-devtools和Thymeleaf模板将不会实时重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

以前我一直在使用Spring Boot 1.2.7(重新加载Spring),并且能够在不重新启动Spring Boot应用程序的情况下即时修改模板.

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.

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

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

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

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.

在Netbeans中,在项目属性"->生成"->编译"下,选中了保存时编译"复选框. 我通过修改.java文件并检查/target/classes中的时间戳验证了此方法确实有效.

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.

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

我的pom.xml中有以下行为(包括其他事件,由于不相关而被排除):

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>

有了这个,我应该被设置为 Spring Boot博客提到以下内容:

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

当包含spring-boot-devtools模块时,任何对classpath文件的更改都会自动触发应用程序重新启动."

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

和类似的评论在 Spring中进行了说明引导官方文档.

我尝试使用带有版本标签1.2.7.RELEASE的spring-boot-maven-plugin,保存模板后,在浏览器中可以立即看到对Thymeleaf模板的更改.看来,至少Thymeleaf模板的问题不是由于spring-boot-devtools,而是由于spring-bot-maven-plugin.

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.

问题可以分为两部分:

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

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.

更新:验证未加载devtools(主线程名称未重新启动Main). 解决2),将Netbeans项目属性中运行项目操作"中的执行目标"更改为以下内容:

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

旧执行目标为package spring-boot:run.仔细搜索一下发现,当使用spring-boot:run运行项目时,其他人在使用spring-boot-devtools时遇到了问题.

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.

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

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

推荐答案

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

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 代替package spring-boot:run启用Spring Boot Devtools并按预期方式重新启动.

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.

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

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.

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

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>

这篇关于使用Spring Boot 1.3,当在Netbeans中进行更改时,spring-boot-devtools和Thymeleaf模板将不会实时重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆