Grails环境配置与环境打包

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/IT_hejinrong/article/details/82622675

         grails可以配置多个环境,开发、测试、生产环境,只需要在application.yml中配置即可,打包方式分别为:

          grails dev war

          grails test war

          grails prod war

每个环境配置

Grails支持每个环境配置的概念。目录中的application.ymlapplication.groovy文件grails-app/conf可以使用YAML或ConfigSlurper提供的语法来使用每个环境配置。作为示例,请考虑application.ymlGrails提供的以下默认定义:

 

environments:
    development:
        dataSource:
            dbCreate: update
            url: jdbc:oracle:thin:@114.55.235.233:1540:HSZF
            logSql: true
    test:
        dataSource:
            dbCreate: update
            url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    production:
        dataSource:
            dbCreate: update
            #jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
            url: jdbc:oracle:thin:@114.55.235.233:1540:HSZF
            properties:
                jmxEnabled: true
                initialSize: 5
                maxActive: 50
                minIdle: 5
                maxIdle: 25
                maxWait: 10000
                maxAge: 600000
                timeBetweenEvictionRunsMillis: 5000
                minEvictableIdleTimeMillis: 60000
                validationQuery: SELECT 1
                validationQueryTimeout: 3
                validationInterval: 15000
                testOnBorrow: true
                testWhileIdle: true
                testOnReturn: false
                jdbcInterceptors: ConnectionState
                defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

以上可以用Groovy语法表示application.groovy如下:

dataSource {
    pooled = false
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
}
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"
        }
    }
}

注意如何在顶层提供通用配置,然后一个environments块指定每个环境设置的属性dbCreate和url属性DataSource。

包装和运行不同的环境


Grails的命令行具有内置功能,可以在特定环境的上下文中执行任何命令。格式为:

grails <<environment>> <<command name>>
此外,已知有以Grails的3个预设环境:dev,prod,和test为development,production和test。例如,要为test您将运行的环境创建WAR :

grails test war
要定位其他环境,您可以将grails.env变量传递给任何命令:

grails -Dgrails.env=UAT run-app


程序化环境检测


在您的代码中,例如在Gant脚本或引导类中,您可以使用Environment类检测环境:

import grails.util.Environment

...

switch (Environment.current) {
    case Environment.DEVELOPMENT:
        configureForDevelopment()
        break
    case Environment.PRODUCTION:
        configureForProduction()
        break
}


每个环境引导


当您的应用程序在每个环境的基础上启动时,通常需要运行代码。为此,您可以使用该grails-app/init/BootStrap.groovy文件对每个环境执行的支持:

def init = { ServletContext ctx ->
    environments {
        production {
            ctx.setAttribute("env", "prod")
        }
        development {
            ctx.setAttribute("env", "dev")
        }
    }
    ctx.setAttribute("foo", "bar")
}


通用环境执行


前面的BootStrap示例在grails.util.Environment内部使用该类来执行。您也可以自己使用此类来执行您自己的特定于环境的逻辑:

Environment.executeForCurrentEnvironment {
    production {
        // do something in production
    }
    development {
        // do something only in development
    }
}

猜你喜欢

转载自blog.csdn.net/IT_hejinrong/article/details/82622675