Grails 3 - 在 Bootstrap.groovy 中读取类路径资源

Grails 3 - Read classpath resource in Bootstrap.groovy

我正在将 Grails 2.5 应用程序迁移到 Grails 3.0.9,在读取 grails-app/init/BootStrap.groovy 中的 classpath 资源时遇到一些问题。

在 Grails 2.5.x 中,当将资源文件放入与 [=15] 相同的文件夹时,可以使用 Spring ClassPathResource 访问资源(例如文本文件) =]

def file1 = new ClassPathResource('my-textfile.txt').file

在 Grails 3 中,当我使用 grails run-app 启动我的应用程序时它可以工作。所有资源均取自src/main/resources。但是,如果我在 IntelliJ 中使用 Spring 引导应用程序(位于 /grails-app/init/a/b/Application.groovy)class 启动应用程序,BootStrap.groovy 将不再找到资源。

有人知道如何在使用 Application.groovy 启动应用程序时在 Grails 3.0.9 中的 BootStrap.groovy 中访问这些文件吗?

根据 Gradle 约定,我将资源放在 src/main/resource 下,因为它们需要在运行时访问。

由于某些原因,我的 grails-app/init/a/b/Application.groovy 找不到这些资源,所以我不得不将符号 link 放置到 grails-app/init/a/b。之后一切正常。

class BootStrap {

     def init = { servletContext ->

        def config = new ConfigSlurper().parse(new File("${System.properties['user.dir']}/grails-app/conf/test.groovy").toURL())
        println config
        println config.hfy.test
    }
    def destroy = {
    }
}

${System.properties['user.dir']} : 项目目录