将配置文件application.yml转换为Grails 3.x中的application.groovy [英] convert configuration file application.yml to application.groovy in Grails 3.x
问题描述
我正在尝试创建一个简单的Grails 3项目,并且遇到了一些非常简单的事情。所以我希望我的数据源属性来自我在IntelliJ IDE中设置的VM选项。在使用Grails 2.x之前,我只是习惯于这样做:
environments {
development {
//数据库连接属性
def dbserver = System.properties.getProperty('dbserver')
def dbport = System.properties.getProperty('dbport')
...... ......
dataSource {
url:jdbc:sqlserver:// $ {dbserver}:$ {dbport}; databaseName = $ {dbname}
}
}
现在我有application.yml,如何访问System.properties并将其嵌入在yml中?我已经读过,如果YML不支持它,我们可以使用application.groovy,在这种情况下,这就是application.groovy的样子:
grails {
p>
profile ='web'
codegen {
defaultPackage ='defPack'
}
}
info {
app {
name ='@ info.app.name @'
version ='@info.app .version @'
grailsVersion ='@ info.app.grailsVersion @'
}
}
spring {
groovy {
template ['check-template-location'] = false
}
}
hibernate {
naming_strategy ='org.hibernate.cfg.DefaultNamingStrategy'
缓存{
queries = false
}
}
grails {
mime {
disable {
accept {
header {
userAgents = ['Gecko','WebKit','Presto','Trident']
}
}
}
类型{
all ='* / *'
atom ='application / atom + xml'
css ='text / css'
csv ='text / csv'
form ='application / x-www-form-urlencoded'
html = ['text / html','application / xhtml + xml']
js ='text / javascript'
json = ['application / json','text / json']
multipartForm ='multipart / form-data'
rss ='application / rss + xml'
text ='text / plain'
hal = ['application / hal + json','application / hal + xml']
xml = ['text / xml','application / xml']
$ b $ urlmap {b $ b cache {
maxsize = 1000
}
}
控制器{
defaultScope =' singleton'
}
转换器{
encoding ='UTF-8'
}
views {
default {codec ='html'}
gsp {
encoding ='UTF-8'
htmlcodec ='xml'
编解码器{
表达式='html'
scriptlets ='html'
taglib ='none'
staticparts ='none'
}
}
}
}
dataSource {
pooled = true
jmxExport = true
driverClassName ='com.microsoft.sqlserver.jdbc .SQLServerDriver'
dbCreate =''
username ='someUsername'
password ='somePass'
}
environments {
development {
dataSource {
url ='jdbc:sqlserver:// localhost:1234; databaseName = someDbName;'
}
}
}
谢谢。
更新:
application.groovy没有被默认引用,即使我删除了application.yml
解决方案<原来我有一个问题,我需要在引号中加上'default'关键字。像:
grails {
profile ='web'
codegen {
defaultPackage =' defPack'
}
}
信息{
app {
name ='@ info.app.name @'
version =' @ info.app.version @'
grailsVersion ='@ info.app.grailsVersion @'
}
}
spring {
groovy {
template ['check-template-location'] = false
}
}
hibernate {
naming_strategy ='org.hibernate.cfg.DefaultNamingStrategy'
cache {
queries = false
}
}
grails {
mime {
disable {
accept {
header {
userAgents = ['Gecko','WebKit','Presto','Trident']
}
}
}
types {
all ='* / *'
atom ='application / ato m + xml'
css ='text / css'
csv ='text / csv'
form ='application / x-www-form-urlencoded'
html = [ 'text / html','application / xhtml + xml']
js ='text / javascript'
json = ['application / json','text / json']
multipartForm = 'multipart / form-data'
rss ='application / rss + xml'
text ='text / plain'
hal = ['application / hal + json','application / hal + xml']
xml = ['text / xml','application / xml']
}
}
urlmapping {
cache {
maxsize = 1000
}
}
控制器{
defaultScope ='singleton'
}
转换器{
编码='UTF-8'
}
views {
'default'{codec ='html'} //这是错误的来源
gsp {
encoding ='UTF-8'
htmlcodec ='xml'
编解码器{
表达式='html'
scriptlets ='html'
taglib ='none'
staticparts ='none'
}
}
}
}
def dbserver = System.properties.getProperty ('dbserver')
def dbport = System.properties.getProperty('dbport')
def dbusername = System.properties.getProperty('dbusername')
def dbpassword = System.properties。 getProperty('dbpassword')
def dbname = System.properties.getProperty('dbname')
dataSource {
pooled = true
jmxExport = true
driverClassName ='com.microsoft.sqlserver.jdbc.SQLServerDriver'
dbCreate =''
username = dbusername
password = dbpassword
}
environments {
开发{
dataSource {
url ='jdbc:sqlserver:// $ {dbserver}:$ {dbport}; databaseName = $ {dbname}'
}
}
}
I am trying to create a simple Grails 3 project and got stuck with something really simple. So I want my data source properties to come from VM options that I set in my IntelliJ IDE. Before in Grails 2.x, I just used to do something like:
environments { development{ //Database connection properties def dbserver = System.properties.getProperty('dbserver') def dbport = System.properties.getProperty('dbport') ............ dataSource { url: "jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname} } }
Now that I have application.yml, how do I access "System.properties" and embed it in yml? I have read that we can instead use application.groovy if YML doesn't support it, in that case this is what the application.groovy looks like :
grails { profile = 'web' codegen { defaultPackage = 'defPack' } } info { app { name = '@info.app.name@' version = '@info.app.version@' grailsVersion = '@info.app.grailsVersion@' } } spring { groovy { template['check-template-location'] = false } } hibernate { naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy' cache { queries = false } } grails { mime { disable { accept { header { userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident'] } } } types { all = '*/*' atom = 'application/atom+xml' css = 'text/css' csv = 'text/csv' form = 'application/x-www-form-urlencoded' html = ['text/html', 'application/xhtml+xml'] js = 'text/javascript' json = ['application/json', 'text/json'] multipartForm = 'multipart/form-data' rss = 'application/rss+xml' text = 'text/plain' hal = ['application/hal+json', 'application/hal+xml'] xml = ['text/xml', 'application/xml'] } } urlmapping { cache { maxsize = 1000 } } controllers { defaultScope = 'singleton' } converters { encoding = 'UTF-8' } views { default { codec = 'html' } gsp { encoding = 'UTF-8' htmlcodec = 'xml' codecs { expression = 'html' scriptlets = 'html' taglib = 'none' staticparts = 'none' } } } } dataSource { pooled = true jmxExport = true driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' dbCreate = '' username = 'someUsername' password = 'somePass' } environments { development { dataSource { url = 'jdbc:sqlserver://localhost:1234;databaseName=someDbName;' } } }
Thanks.
UPDATE:
application.groovy is not being taken in by default, even when I removed application.yml
解决方案Turned out I had an issue, I needed to put 'default' keyword within quotes. Like :
grails { profile = 'web' codegen { defaultPackage = 'defPack' } } info { app { name = '@info.app.name@' version = '@info.app.version@' grailsVersion = '@info.app.grailsVersion@' } } spring { groovy { template['check-template-location'] = false } } hibernate { naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy' cache { queries = false } } grails { mime { disable { accept { header { userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident'] } } } types { all = '*/*' atom = 'application/atom+xml' css = 'text/css' csv = 'text/csv' form = 'application/x-www-form-urlencoded' html = ['text/html', 'application/xhtml+xml'] js = 'text/javascript' json = ['application/json', 'text/json'] multipartForm = 'multipart/form-data' rss = 'application/rss+xml' text = 'text/plain' hal = ['application/hal+json', 'application/hal+xml'] xml = ['text/xml', 'application/xml'] } } urlmapping { cache { maxsize = 1000 } } controllers { defaultScope = 'singleton' } converters { encoding = 'UTF-8' } views { 'default' { codec = 'html' }//THIS WAS THE SOURCE OF ERROR gsp { encoding = 'UTF-8' htmlcodec = 'xml' codecs { expression = 'html' scriptlets = 'html' taglib = 'none' staticparts = 'none' } } } } def dbserver = System.properties.getProperty('dbserver') def dbport = System.properties.getProperty('dbport') def dbusername = System.properties.getProperty('dbusername') def dbpassword = System.properties.getProperty('dbpassword') def dbname = System.properties.getProperty('dbname') dataSource { pooled = true jmxExport = true driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver' dbCreate = '' username = dbusername password = dbpassword } environments { development { dataSource { url = 'jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname}' } } }
这篇关于将配置文件application.yml转换为Grails 3.x中的application.groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!