在 Grails 3.x 中将配置文件 application.yml 转换为 application.groovy
convert configuration file application.yml to application.groovy in Grails 3.x
我正在尝试创建一个简单的 Grails 3 项目,但遇到了一些非常简单的问题。所以我希望我的数据源属性来自我在 IntelliJ IDE 中设置的 VM 选项。在 Grails 2.x 之前,我只是习惯于做这样的事情:
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}
}
}
现在我有了 application.yml,如何访问 "System.properties" 并将其嵌入到 yml 中?我读过,如果 YML 不支持,我们可以改用 application.groovy,在这种情况下,application.groovy 看起来像这样:
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;'
}
}
}
谢谢。
更新:
默认情况下 application.groovy 不会被接受,即使我删除了 application.yml
原来我遇到了问题,我需要将 'default' 关键字放在引号内。喜欢:
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}'
}
}
}
可能无法直接回答您的问题。您可以通过将以下内容添加到 build.gradle
来访问 application.yml 中的系统属性
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperties = System.properties
我正在尝试创建一个简单的 Grails 3 项目,但遇到了一些非常简单的问题。所以我希望我的数据源属性来自我在 IntelliJ IDE 中设置的 VM 选项。在 Grails 2.x 之前,我只是习惯于做这样的事情:
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}
}
}
现在我有了 application.yml,如何访问 "System.properties" 并将其嵌入到 yml 中?我读过,如果 YML 不支持,我们可以改用 application.groovy,在这种情况下,application.groovy 看起来像这样:
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;'
}
}
}
谢谢。
更新:
默认情况下application.groovy 不会被接受,即使我删除了 application.yml
原来我遇到了问题,我需要将 'default' 关键字放在引号内。喜欢:
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}'
}
}
}
可能无法直接回答您的问题。您可以通过将以下内容添加到 build.gradle
来访问 application.yml 中的系统属性tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperties = System.properties