【问题标题】:One Gradle Thymeleaf project reloads on resource change, the other relaunches一个 Gradle Thymeleaf 项目在资源更改时重新加载,另一个重新启动
【发布时间】:2017-05-18 06:41:21
【问题描述】:

编辑:我已经稍微缩小了行为范围,不确定是否有办法完成我需要的。

事实证明,我的应用在源代码树的下一层有 Thymeleaf 模板:

src/main/resources/tools-server/templates

我在我的tools-server.yml 文件中设置了它,该文件在应用程序启动时显式加载。从我的配置中删除该规范,并将templates 目录上移一级到

src/main/resources/templates

允许spring-boot-devtools 简单地重新加载模板而无需重新启动应用程序。我想我会在项目中提交一个错误,除非有办法解决。


我还在掌握 Spring Boot 的窍门,所以请耐心等待。在过去的几个月里,我创建了两个项目,每个项目都从网上找到的不同示例开始。

关于重新加载 Thymeleaf 模板,第一个项目在它们发生变化时巧妙地完成了,当模板发生变化时发出两条日志消息,仅此而已。另一个完全停止并重新启动应用程序,这会导致问题,因为它会重新创建临时安全密码等(也需要更长的时间)。

这两个gradle.build 文件几乎相同,只是依赖关系略有不同。我不确定这些是否是导致不同行为的差异。

工作的人:

buildscript
{
    ext
    {
        springBootVersion = "1.4.3.RELEASE"
    }
    repositories
    {
        mavenCentral()
    }
    dependencies
    {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath group: "com.layer", name: "gradle-git-repo-plugin", version: "2.0.2"
    }
}

apply plugin: "git-repo"
apply plugin: "java"
apply plugin: "maven"
apply plugin: "spring-boot"

jar
{
    baseName = "HOA"
    version = "0.0.1-SNAPSHOT"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories
{
    mavenCentral()
    maven { url "https://maven.atlassian.com/3rdparty/" }
    maven { url "https://mvnrepository.com/artifact/" }
    maven { url "http://repo.spring.io/snapshot" }
    maven { url "http://repo.spring.io/milestone" }
}


dependencies
{
    compile group: "org.mindrot",                   name: "jbcrypt",                    version: "0.4-atlassian-1"
    compile group: "org.eclipse.persistence",       name: "javax.persistence",          version: "2.1.1"
    compile group: "org.springframework.data",      name: "spring-data-jpa",            version: "1.10.4.RELEASE"
    compile group: "org.springframework.hateoas",   name: "spring-hateoas",             version: "0.21.0.RELEASE"

    compile group: "com.h2database",                name: "h2",                         version: "1.4.192"

    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.springframework.boot:spring-boot-starter-aop")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-groovy-templates")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-web")

    //  Automated Testing

    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.springframework.restdocs:spring-restdocs-mockmvc")
}

dependencyManagement
{
    imports
    {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR1"
    }
}


compileJava
{
    options.compilerArgs << "-Xlint:all" << "-Xdiags:verbose"
}

bootRepackage
{
    mainClass = "com.latencyzero.hoa.Application"
}

bootRun
{
    addResources = true
}

凌乱的:

buildscript
{
    ext
    {
        springBootVersion = '1.4.3.RELEASE'
    }

    repositories
    {
        mavenCentral()
    }

    dependencies
    {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

archivesBaseName = 'toolcrib'

compileJava
{
    options.compilerArgs << "-Xlint:all" << "-Xdiags:verbose"
}

jar
{
    manifest
    {
        attributes 'Implementation-Title': 'ToolCrib',
                    'Implementation-Version': version
    }
}

repositories
{
    mavenCentral()
}

dependencyManagement
{
    imports
    {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR3'
    }
}

dependencies
{
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')

    runtime('org.postgresql:postgresql')

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

springBoot
{
    mainClass = "com.latencyzero.toolcrib.services.tools.ToolsServer"
}


bootRun
{
    addResources = true
}

感谢您的任何见解!

【问题讨论】:

    标签: gradle spring-boot thymeleaf


    【解决方案1】:

    你有没有a look to the documentation

    默认情况下,更改 /META-INF/maven、/META-INF/resources、/resources、/static、/public 或 /templates 中的资源不会触发重新启动,但会触发实时重新加载。

    还有

    如果您想自定义这些排除项,您可以使用 spring.devtools.restart.exclude

    还有spring.devtools.restart.additional-exclude 可以添加更多排除项并保留默认值。在您的情况下,您应该将以下内容添加到您的配置中:

    spring.devtools.restart.additional-exclude=classpath:/tools-server/templates/ 
    

    【讨论】:

    • 该部分似乎不相关,因为我正在浏览代码以找到答案。我不想将我的文件排除在触发重新加载之外。此外,它紧接在介绍类重载之后,除了不是关于模板(即不是由类加载器加载的代码)之外,还暗示它是用于构建的东西(在build 目录中)。我希望它在我的源目录中查找文件,如果它们位于正确的位置,它会这样做,但它似乎很神奇且无法解释它如何以及何时选择这样做。
    • 如果我排除 classpath:/...,它是否仍会在原始源位置查找更改,还是仅在某种构建将这些文件复制到构建位置时才会看到它们?
    • “我不想从触发重新加载中排除我的文件” 该属性称为“restart.exclude”。我不知道还能说什么,这正是你要问的。如果文档令人困惑,请告诉我们如何进行调整。
    • 您提到了一个根本不存在的功能。如果您将模板放在默认位置,您还必须更新类路径以使重新加载(!=重新启动)生效。我创建了this issue 来查看文档。
    • 有几种方法可以完成这项工作,但这个对话是在循环中进行的。只要您按照我在回答中告诉您的操作,devtools 关于模板的位置就没有什么不同。如果您想了解更多详细信息,让我们切换到聊天。
    猜你喜欢
    • 2020-09-01
    • 2016-02-13
    • 2017-09-10
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2013-02-22
    相关资源
    最近更新 更多