- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Externalized Configuration 中的第 24.3 节表示 .properties
或 .yml
文件可用于外部配置,但我希望我的外部配置为 .groovy
文件就像我的 application.groovy
我已经从 .yml
转换的文件.我怎样才能做到这一点?
Grails 版本 3.2.0.M2
更新:
我能够根据@Michal_Szulc 提供的答案完成这项工作
请注意 ConfigSlurper
需要当前环境才能正常工作。另请注意,这些更改将对 my_grails_app/grails-app/init/my_grails_app/Application.groovy
进行。文件,而不是 my_grails_app/grails-app/conf/application.groovy
如果您从 .yml
转换而来,您可能会拥有该文件配置到 .groovy
配置。
package my_grails_app
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.context.EnvironmentAware
import org.springframework.core.env.Environment
import org.springframework.core.env.MapPropertySource
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main( String[] args ) {
GrailsApp.run( Application, args )
}
@Override
void setEnvironment( Environment environment ) {
def appName = grails.util.Metadata.current.getApplicationName()
// The value of this environment variable should be the absolute path to an external .groovy config file like /opt/my_grails_app/my_grails_app-config.groovy.
def envVarName = "MY_GRAILS_APP_CONFIG"
// If you don't want to use an environment variable you can specify your external .groovy config file here, but the environment variable will still be checked first.
def dfltAppConfigFileName = "/opt/${ appName }/${ appName }-config.groovy"
def appConfigFile = null
def loadDfltConfig = false
// Try to load config specified by the environment variable first
println "Checking the environment variable ${ envVarName } for the configuration file location..."
def envVarVal = System.getenv( envVarName ) ?: System.getProperty( envVarName )
if( envVarVal ) {
appConfigFile = new File( envVarVal )
if( !appConfigFile.exists() ) {
println "The configuration file ${ appConfigFile } specified by the environment variable ${ envVarName } does not exist. Checking for the default configuration file ${ dfltAppConfigFileName } instead..."
appConfigFile = null
loadDfltConfig = true
}
} else {
println "The environment variable ${ envVarName } which specifies the configuration file to be loaded does not exist. Checking for the default configuration file ${ dfltAppConfigFileName } instead..."
appConfigFile = null
loadDfltConfig = true
}
// Try loading the default config file since we couldn't find one specified by the environment variable
if( loadDfltConfig ) {
appConfigFile = new File( dfltAppConfigFileName )
if( !appConfigFile.exists() ) {
println "The default configuration file ${ dfltAppConfigFileName } does not exist."
appConfigFile = null
}
}
// Load the config file if it exists, otherwise exit
if( appConfigFile ) {
println "Loading configuration file ${ appConfigFile }"
def config = new ConfigSlurper( environment.activeProfiles.first() ).parse( appConfigFile.toURI().toURL() )
environment.propertySources.addFirst( new MapPropertySource( "${ appName }-config", config ) )
} else {
println "No configuration file found. Exiting."
System.exit( 1 )
}
最佳答案
我找到了 this thread和报价格雷姆·罗彻 :
Grails 3 uses Spring's property sources concept, so it will resolve properties from the system, the environment and finally the application.yml/application.groovy
class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main(String[] args) {
GrailsApp.run(Application)
}
@Override
void setEnvironment(Environment environment) {
//Set up Configuration directory
def krakenHome = System.getenv('KRAKEN_HOME') ?: System.getProperty('KRAKEN_HOME') ?: "/opt/kraken"
println ""
println "Loading configuration from ${krakenHome}"
def appConfigured = new File(krakenHome, 'KrakenConfig.groovy').exists()
println "Loading configuration file ${new File(krakenHome, 'KrakenConfig.groovy')}"
println "Config file found : " + appConfigured
if (appConfigured) {
def config = new ConfigSlurper().parse(new File(krakenHome, 'KrakenConfig.groovy').toURL())
environment.propertySources.addFirst(new MapPropertySource("KrakenConfig", config))
}
}
}
关于grails - 如何在 Grails 3 中使用外部 .groovy 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645019/
我有一些库脚本:lib1.groovy: def a(){ } lib2.groovy: def b(){ } lib3.groovy: def c(){ } 并想在其他脚本中使用它们:配置文件: a
我有下面的 Groovy 脚本,我需要将它放在集中式 Groovy 库中,然后从 Ready API 项目中的任何脚本访问 Groovy 中提到的类 路径 : D:\GroovyLib\com\Lin
看完后this link ,我想尝试Groovy++,但我有一个担心; Groovy 的所有语法在 Groovy++ 中都有效吗? 例如,我可以在 Groovy 中执行此操作: def list =
我在 Spring-boot 应用程序中混合了 Groovy 和 Java。休息 Controller 和数据访问是用 Groovy 编写的。配置主要使用Java。 根据 logback 文档,如果类
我已阅读how to simply import a groovy file in another groovy script 我想在一个 groovy 文件中定义常用函数,并从其他 groovy 文
你知道,我也知道,只要只有一个是公共(public)的,就可以用 Java 实现。但是,在 Groovy 中可以这样做吗?如果是的话,在什么条件下? 最佳答案 Java 和 Groovy 之间在可以放
~/groovy % tree . ├── lib │ ├── GTemplate.class │ └── GTemplate.groovy └── Simple.groovy class
给定一个具有属性和构造函数的对象,我希望将构造函数参数复制到属性中,然后在构造函数中做一些额外的工作。 import groovy.transform.TupleConstructor @TupleC
我会提前道歉,我是 groovy 的新手。我的问题是我有 3 个执行不同功能的 groovy 脚本,我需要从我的主 groovy 脚本中调用它们,使用脚本 1 的输出作为脚本 2 的输入和脚本 2 的
我想在静态闭包中存储一些属性,然后在方法调用期间访问它们: class Person { static someMap = { key1: "value1", key2: "value2" } }
Groovy 是否有安全范围运算符? 例如,如果我有, [1,2,3][0..10] Groovy 会抛出一个 java.lang.IndexOutOfBoundsException: 有没有索引安全
在 Groovy 中使用 Maps/JsonBuilder 处理一些翻译/映射功能。 是否有可能(无需在 map 文字创建之外创建额外的代码).. 有条件地包含/排除某些键/值对?一些事情沿着以下路线
不知道我是否正确询问,但是我有类似以下内容: def x = 1 if (x == 1) { def answer = "yes" } println answer 我收到错误
我不明白 groovy 打字是如何工作的。在 wikipedia据说它具有很强的类型,但我可以在解释器上完美地做到这一点: 1 + '1' ==> 11 所以也许我很困惑,我不明白弱类型是什么,但我想
我对函数式编程概念非常陌生,正在观看 Neil Ford 在 youtube 中的演讲。 .在那里他谈到了一个计数器来演示一段代码而不使用全局状态(在 20:04)。来自 Java 世界,我很难理解这
我有两个问题。 我执行以下代码来查找 $ 的 ASCII 值: def a = "\$" def b = (int)a println b //prints 36 好吧,我对答案很满意。但是当我尝试像
只是想知道 时髦 像这样与默认值进行值匹配的方法? if(params.max != 10 && params.max != 20 && params.max != 30){ params.m
我最近正在读《行动中的格鲁夫》。在第7章中,它介绍了*。运算符(operator) 。当我运行有关此运算符的代码时,我会遇到一些错误。 class Invoice {
是否有易于阅读的方法或一些聪明的方法来制作 combination Groovy 中的元素?我知道 Iterable#combinations或 GroovyCollections#combinati
最近我下载了 Groovy-2.3.6 并尝试在 Linux 系统上安装它。我按照 http://groovy-lang.org/install.html 的说明进行操作.我设置了我的 GROOVY_
我是一名优秀的程序员,十分优秀!