【问题标题】:convert configuration file application.yml to application.groovy in Grails 3.x在 Grails 3.x 中将配置文件 application.yml 转换为 application.groovy
【发布时间】:2016-08-08 12:52:20
【问题描述】:

我正在尝试创建一个简单的 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.yml,默认情况下也不会引入 application.groovy

【问题讨论】:

  • 对于最后一部分,你必须引用他们:userAgents = ['Gecko', 'WebKit']
  • @IanRoberts,谢谢,我会更新我的帖子。

标签: grails grails-3.0


【解决方案1】:

原来我遇到了问题,我需要将“默认”关键字放在引号内。喜欢:

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.groovy 文件的?意思是你把它变成了你自己.. ?正如您在更新中所说,默认情况下不需要!并且比在 application.groovy 文件中可以访问“System.properties”和其他 Java/Groovy 相关对象。仅供参考,我还需要使用自己创建的 groovy 类对象[需要导入类]。能否请您指出一些解决方案!
  • 是的,您必须手动添加它,然后删除您的 application.yml 文件,否则我认为优先级是 yml 版本而不是 groovy 版本。是的,在 groovy 文件中你可以使用任何 System.properties,我没有尝试过导入类,但你可以尝试“导入”看看会发生什么。
【解决方案2】:

可能不是您问题的直接答案。您可以通过在 build.gradle 中添加以下内容来访问 application.yml 中的系统属性

tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperties = System.properties

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

【讨论】:

    猜你喜欢
    • 2015-12-03
    • 2020-11-24
    • 1970-01-01
    • 2016-06-18
    • 2015-06-13
    • 1970-01-01
    • 2016-05-05
    • 2017-07-21
    • 2015-06-16
    相关资源
    最近更新 更多