Spring Boot DevTools 在 Eclipse 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43147157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:24:21  来源:igfitidea点击:

Spring Boot DevTools not working in Eclipse

javaeclipsemavenspring-bootspring-boot-devtools

提问by John Mendes

I built an application using Spring, JPA, MySQL and Web. I developed a static page in template folder normally and it works.

我使用 Spring、JPA、MySQL 和 Web 构建了一个应用程序。我通常在模板文件夹中开发了一个静态页面,并且可以正常工作。

But, when I change something on static page, I can't reload it with changes. Then, I open the pom.xml and added

但是,当我在静态页面上更改某些内容时,我无法通过更改重新加载它。然后,我打开 pom.xml 并添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

I restart the application, but, still not working when I make some changes on the static page.

我重新启动了应用程序,但是当我在静态页面上进行一些更改时仍然无法正常工作。

Is there something more to do?

还有其他事情要做吗?

My POM.xml

我的 POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.engsoftware</groupId>
    <artifactId>cobranca</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Cobranca</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

采纳答案by John Mendes

I followed this article https://github.com/spring-projects/spring-boot/issues/7479

我跟着这篇文章https://github.com/spring-projects/spring-boot/issues/7479

So, to devtools works, you must add:

因此,要使 devtools 工作,您必须添加:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>runtime</scope>
</dependency>

The secret is add Optional True and Scope runtime.

秘诀是添加 Optional True 和 Scope 运行时。

回答by user204069

In your eclipse top menu your "Project" -> "Build Automatically" ON ?

在您的 eclipse 顶部菜单中,您的“项目”->“自动构建”是否开启?

回答by gorcajo

This question is already answered, but for me didn't worked exactly as the accepted answer or other answers tells.

这个问题已经得到了回答,但对我来说并没有完全按照接受的答案或其他答案所说的那样工作。

I've got devtools working in the following way:

我有 devtools 以下列方式工作:

1) Using the devtools dependency as following:

1) 使用 devtools 依赖如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>runtime</scope>
</dependency>

2) Deleting the Spring Maven cache, in Linux it would be:

2)删除Spring Maven缓存,在Linux中它会是:

rm -rf ~/.m2/repository/org/springframework/*

3) Back into Eclipse, pressing Alt+F5 and forcing to clean the project re-downloading every dependency from Maven to your cache.

3) 返回 Eclipse,按 Alt+F5 并强制清理项目,将每个依赖项从 Maven 重新下载到缓存。

The key is to set the optionalflag to trueinto the devtools' dependency ANDwiping the Maven cache.

关键是将optional标志设置true为 devtools 的依赖项擦除 Maven 缓存。

Hope this is helpful to someone.

希望这对某人有帮助。

回答by Durja Arai

In addition to the answers given above, you also need to have Build Automatically enabled in Eclipse. It doesn't matter to Dev Tools if your java files are updated, the only thing it looks for is the .class, resource file changes.

除了上面给出的答案,您还需要在 Eclipse 中启用自动构建。Dev Tools 是否更新您的 java 文件并不重要,它唯一要查找的是 .class 资源文件更改。

For IntelliJ, refer to the diagram below.

对于 IntelliJ,请参考下图。

enter image description here

在此处输入图片说明

回答by Jason White

According to the Spring Boot docs:

根据 Spring Boot 文档:

Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes. Note that certain resources such as static assets and view templates do not need to restart the application.

每当 classpath 上的文件发生更改时,使用 spring-boot-devtools 的应用程序将自动重新启动。在 IDE 中工作时,这可能是一个有用的功能,因为它为代码更改提供了非常快速的反馈循环。默认情况下,将监视类路径上指向文件夹的任何条目的更改。请注意,某些资源(例如静态资产和视图模板)不需要重新启动应用程序。

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart

Templates and static assets do not need a restart. Most likely your browser is caching the templates and using the cached version instead of requesting the new template. If you clear the browsers cache, you should see the updated template.

模板和静态资产不需要重新启动。很可能您的浏览器正在缓存模板并使用缓存版本而不是请求新模板。如果您清除浏览器缓存,您应该会看到更新后的模板。



EDIT:

编辑:

Depending on your template technology you're using, you will need to set a property in your properties file to disable the template cache

根据您使用的模板技术,您需要在属性文件中设置一个属性以禁用模板缓存

# Thymeleaf
spring.thymeleaf.cache = false

#FreeMarker
spring.freemarker.cache = false

#Groovy
spring.groovy.template.cache = false

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html#howto-reload-static-content

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html#howto-reload-static-content

回答by ABC

Even after trying above mentioned answers, if it is still not working just try enabling build automatically for the project in eclipse. It worked for me after that.

即使在尝试了上述答案之后,如果它仍然不起作用,请尝试在 Eclipse 中为项目自动启用构建。在那之后它对我有用。

回答by Ezhil vikraman

This worked for me. Add the below line in application.properties file,

这对我有用。在 application.properties 文件中添加以下行,

spring.devtools.restart.enabled=true