将配置文件application.yml转换为Grails 3.x中的application.groovy

时间:2015-05-05 15:34:27

标签: grails grails-3.0

我正在尝试创建一个简单的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

2 个答案:

答案 0 :(得分:3)

原来我遇到了问题,我需要提出默认问题'引号内的关键字。喜欢:

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}'
        }
    }
}

答案 1 :(得分:1)

可能不是您问题的直接答案。您可以通过在build.gradle

下面添加以下内容来访问application.yml中的系统属性
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperties = System.properties

参考:https://github.com/grails/grails-core/issues/9086