问题描述
|
是否可以通过某种方式访问
BuildConfig.groovy
中的Config.groovy
中指定的属性?
在打包过程中,我需要复制一些引导文件,并且目标目录取决于指定的环境。由于我需要在应用程序引导期间访问这些文件,因此我想在“ 0”中定义路径,而不要复制它。
解决方法
访问诸如“ 0”中定义的“ 3”之类的属性的常用方法是
def bootStrapPath = org.codehaus.groovy.grails.commons.ConfigurationHolder.config.bootstrap.path
我还没有尝试过BuildConfig.groovy
,但是如果它不起作用,那是因为执行executed1ѭ时尚未读取read0ѭ。在这种情况下,您需要自己阅读。 ѭ9的实现应向您展示如何执行此操作。
,如果说打包过程是指生成WAR文件,那么我已经能够使用Grails脚本/事件将文件复制到其他位置。例如,生成WAR时需要将一个文件复制到WEB-INF / classes文件夹,因此我在/ scripts文件夹下创建了一个Events.groovy文件,内容如下:
// Copy liquibase changelog.xml to classpath folder
eventWarStart = {warName ->
if (grailsEnv == \"production\") {
println \"Copying database migration files to classpath!\"
Ant.copy(toFile: \"${classesDirPath}/changelog.xml\",filtering: true,overwrite: true) {
fileset(file: \"${basedir}/grails-app/migrations/changelog.xml\")
}
Ant.copy(toDir: \"${classesDirPath}/releases/\",overwrite: true) {
fileset(dir: \"${basedir}/grails-app/migrations/releases/\")
}
}
}
如您所见,您可以访问环境。
如果这不是您的意思,请忽略我的回答。
,你可以这样做:
System.getProperty(\'grails.env\')
检索当前环境。
,我找到的唯一解决方案是我自己读ѭ0Don(如Don所提到的)。这是我的两行解决方案:
def directory = new File(getClass().protectionDomain.codeSource.location.path).parent
def config = new ConfigSlurper(grailsSettings.grailsEnv).parse(new File(directory + File.separator + \"Config.groovy\").toURI().toURL())
println config.bootstrapPath