在两个共享公共插件的grails应用程序之间共享配置 [英] Share config between two grails apps that share a common plugin
问题描述
我们使用grailsApplication.config。*来配置类似的东西到外部服务的URL。根据应用程序是否在dev / test / qa / staging / prod中运行,这些不同,因此我们使用了Config.groovy中的environments部分。我们将需要为这两个应用程序配置相同的URL /环境。
为了避免重复,我们正在尝试构建一个可以容纳所有共享内容的插件。这适用于服务等,但Grails插件不包含Config.groovy,resources.groovy,所以所有的URL配置等都不能放在插件的Config.groovy中。
有没有一种很好的方式将该配置放在一个地方,并且可以同时用于这两个应用程序?
也许我们可以把它放在某个地方该插件并将它导入到两个应用程序的Config.groovy中?
grails。外部配置文件的config.locations
定义可以包含 java.lang.Class
对象,用于从预编译的Groovy脚本加载配置,以及 file:
或 classpath:
解析Groovy或 .properties
运行时的文件。所以你应该可以在 src / groovy
{插件下的插件中创建一个配置文件{plugin } /src/groovy/com/example/CommonConfiguration.groovy
package com.example
环境{
生产{
...
}
开发{
...
}
}
然后在应用程序的 Config.groovy
文件中包含这个class $ grails.config.locations
grails.config.locations = [com.example.CommonConfiguration]
然而,这意味着当插件的 CommonConfiguration
和主机应用程序的 Config.groovy
都指定了相同属性的值,插件会胜出。为了解决这个问题,你需要在 grails.config.locations
中添加第二个外部函数(可能是另一个 Class
或者URL)
grails.config.locations = [com.example.CommonConfiguration,
file: app-config.groovy]
并在其中放置应用程序配置(如稍后的外部覆盖先前的配置) 。
We will have two apps that both need to use the same services/utilities/code/configuration.
We are using grailsApplication.config.* to configure things like URLs to external services. These are different depending on whether the app is running in dev/test/qa/staging/prod, so we have used the environments section in Config.groovy. We'll need the same URLs/environments configured for both applications.
In order to avoid duplication, we are trying to build a plugin that will hold all the shared stuff. This works for services and such, but Grails plugins do not include Config.groovy, resources.groovy so all the URL configuration and such can't be put in Config.groovy in the plugin.
Is there a nice way to put that configuration in a single place and have it available for both apps?
Perhaps we could put it in some place in the plugin and "import" it into the Config.groovy of both apps?
The grails.config.locations
definition for external configuration files can include java.lang.Class
objects to load configuration from pre-compiled Groovy scripts, as well as file:
or classpath:
URLs to parse Groovy or .properties
files at runtime. So you should be able to create a configuration file in the plugin under src/groovy
{plugin}/src/groovy/com/example/CommonConfiguration.groovy
package com.example
environments {
production {
...
}
development {
...
}
}
and then in the applications' Config.groovy
files include this class in grails.config.locations
grails.config.locations = [com.example.CommonConfiguration]
However this does mean that when the plugin's CommonConfiguration
and the host app's Config.groovy
both specify a value for the same property, the plugin would win. To redress the balance, you'd need to put a second external in grails.config.locations
(which could be another Class
or a URL)
grails.config.locations = [com.example.CommonConfiguration,
"file:app-config.groovy"]
and put app configuration in there (as later externals override earlier ones).
这篇关于在两个共享公共插件的grails应用程序之间共享配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!