如何在 grails 中扩展每个环境的配置

发布于 2024-09-01 19:57:41 字数 442 浏览 2 评论 0原文

根据环境配置,似乎只有 grails.serverURL 和 grails.path 被识别。 bla 和 foo 被忽略并且无法在应用程序中使用 任何人都可以解决这个问题并提供一种根据环境配置 bla 和 foo 的方法吗?

environments {
    production {
        grails.serverURL = "http://alpha.foo.de"
        grails.path = ""
        bla = "text"
        foo= "word"
    }
    test {
        grails.serverURL = "http://test.foo.de"
        grails.path = ""
        bla = "othertext"
        foo= "otherword"
    }
}

It seems that only grails.serverURL and grails.path are recognized as per environment configrautions. bla and foo are ignored and could not be used in application
Anyone could solves this and provide a way to get bla and foo configured per environment?

environments {
    production {
        grails.serverURL = "http://alpha.foo.de"
        grails.path = ""
        bla = "text"
        foo= "word"
    }
    test {
        grails.serverURL = "http://test.foo.de"
        grails.path = ""
        bla = "othertext"
        foo= "otherword"
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悸初 2024-09-08 19:57:41

从 Grails 3.0 开始,Groovy 或 YAML 语法都可以使用。新的主应用程序配置文件是 /conf/application.yml 但您可以继续使用现有的 groovy 配置来定义 /conf/application.groovy 文件。

这是每个环境的数据源定义示例(在 YAML 中):

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb
    test:
        dataSource:
            dbCreate: update
            url: jdbc:h2:mem:testDb
    production:
        dataSource:
            dbCreate: update
            url: jdbc:h2:prodDb
    myenv:
        dataSource:
            dbCreate: update
            url: jdbc:h2:myenvDb

要在特定环境中运行 grails 命令,您可以使用:

grails [environment] [command name]

要针对不同于 dev、test 和 prod 的环境,您可以使用:

grails -Dgrails.env=myenv run-app

请参阅 grails 环境文档此示例应用程序了解更多信息。

Since Grails 3.0, both Groovy or YAML syntax can be used. The new main application configuration file is /conf/application.yml but you can continue to use your existing groovy configuration defining a /conf/application.groovy file.

This is an example of datasource definition per environment (in YAML):

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:h2:mem:devDb
    test:
        dataSource:
            dbCreate: update
            url: jdbc:h2:mem:testDb
    production:
        dataSource:
            dbCreate: update
            url: jdbc:h2:prodDb
    myenv:
        dataSource:
            dbCreate: update
            url: jdbc:h2:myenvDb

To run a grails command in a specific environment you can use:

grails [environment] [command name]

To target an environment different from dev, test and prod you can use:

grails -Dgrails.env=myenv run-app

See the grails environment documentation or this example application for more information.

风吹短裙飘 2024-09-08 19:57:41

所有 ConfigSlurper 变量的范围均受环境限制。您上面显示的内容应该可以正常工作。

当您使用grails run-app时,默认情况下您在开发环境中运行。这可能是你的问题吗?

All the ConfigSlurper variables are scoped by environment. What you've shown above should work fine.

When you use grails run-app, you are running in the development environment by default. Could that be your issue?

心是晴朗的。 2024-09-08 19:57:41

尝试...

> grails prod run-app

这应该给你 bla(文本)和 foo(单词)

> grails test run-app

这应该给你 bla(其他文本)和 foo(其他单词)

Try...

> grails prod run-app

This should give you bla (text) and foo (word)

> grails test run-app

This should give you bla (othertext) and foo (otherword)

三生殊途 2024-09-08 19:57:41

config.groovy设置

environments {
    development {
        grails.serverURL = "http://alpha.foo.de"
        grails.path = "/bar"
        staticServerURL = "http://static.foo.de"
        staticPath = "/static"
    }
}

源代码index.gsp

${grailsApplication.config.staticServerURL}a
${grailsApplication.config.staticPath}b
${grailsApplication.config.grails.serverURL}c
${grailsApplication.config.grails.path}d

用grails run-app启动时打印出什么

a b http://alpha.foo.dec /bard

Config.groovy setting

environments {
    development {
        grails.serverURL = "http://alpha.foo.de"
        grails.path = "/bar"
        staticServerURL = "http://static.foo.de"
        staticPath = "/static"
    }
}

source code index.gsp

${grailsApplication.config.staticServerURL}a
${grailsApplication.config.staticPath}b
${grailsApplication.config.grails.serverURL}c
${grailsApplication.config.grails.path}d

what is printed out when started with grails run-app

a b http://alpha.foo.dec /bard
帥小哥 2024-09-08 19:57:41

检查您的 Config.groovy 是否已导入环境,

import grails.util.Environment

否则,Environment.current 为空。

Check in your Config.groovy that you have the import for Environment

import grails.util.Environment

Otherwise the Environment.current is empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文