`
sillycat
  • 浏览: 2552106 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Grails(3)Guide Book Chapter 4 Configuration

 
阅读更多
Grails(3)Guide Book Chapter 4 Configuration
4.3 The DataSource
If we use a database other than H2 we need a JDBC driver. It's the best to use Ivy to resolve the jar.

The dependency for the MySQL driver like this:
grails.project.dependency.resolution = {
     inherits("global")
     log "warn"
     repositories{
          grailsPlugins()
          grailsHome()
          grailsCentral()
          mavenCentral()
     }
     dependencies {
          runtime 'mysql:mysql-connector-java:5.1.16'
     }
}

If we can't use Ivy then just put the JAR in our project's lib directory.

Database configuration file is her:
grails-app/conf/DataSource.groovy

Take these configuration as instance
dataSource{
     pooled = true
     dbCeate = "update"
     url = "jdbc:mysql://localhost/yourDB"
     driverClassName = "com.mysql.jdbc.Driver"
     dialect = org.hibernate.dialect.MySQL5InnoDBDialect
     username = "yourUser"
     password = "yourPassword"
}

dbCreate ---- create, create-drop, update, validate
create - Drops the existing schema. Creates the schema on startup.
create-drop - Same as create, drops the tables when the application shuts down.
update - Creates missing tables and indexes.
validate - Make no changes to my database, just reports warning.

4.3.1 DataSources and Environments
Just note here, Grails' DataSource definition is 'environment aware'.

4.3.2 JNDI DataSources
Referring to a JNDI DataSource

dataSource {
     jndiName = "java:comp/env/myDataSource"
}

4.3.3 Automatic Database Migration

4.3.4 Transaction-aware DataSource Proxy

4.3.5 Database Console
Config.groovy

environments {
     production {
          grails.serverURL = "http://www.sillycat.com"
          grails.dbconsole.enabled = true
          grails.dbconsole.urlRoot = '/admin/dbconsole'
     }
     development {
          grails.serverURL = "http://localhost:8080/${appname}"
     }
     test {
          grails.serverURL = "http://localhost:8080/${appname}"
     }
}

4.3.6 Multiple Datasources
By default all domain classes share a single DataSource and a single database.
Configuring Additional DataSources
In grails-app/conf/DataSource.groovy

envionments {
     development {
          dataSource {
               dbCreate = "create-drop"
               url = "jdbc:h2:mem:devDb"
          }
          dataSource_lookup {
               dialect = org.hibernate.dialect.MySQLInnoDBDialect
               driverClassName = 'com.mysql.jdbc.Driver'
               username = 'lookup'
               password = '111111'
               url = 'jdbc:mysql://localhost/lookup'    
               dbCreate = 'update'
          }
     }
     …snip…
     production {
          dataSource {
               dbCreate = "update"
               url = "jdbc:h2:prodDb"
          }
          dataSource_lookup {
               dialect = org.hibernate.dialect.Oracle10gDialect
               driverClassName = 'oracle.jdbc.driver.OracleDrvier'
               username = 'lookup'
               password = '111111'
               url = 'jdbc:oracle:thin:@localhost:1521:lookup'
               dbCreate = 'update'
          }
     }
}

Configuring Domain Classes
If a domain class has no DataSource configuration, it defaults to the standard 'dataSource'. If we want to change that, we need to set the datasource property in the mapping block to configure a non-default DataSource.

class ZipCode {
     String code    
     static mapping = {
          datasource 'lookup'
     }
}

A domain class can also use 2 or more DataSources.
static mapping = {
     datasources(['lookup', 'auditing'])
}

DEFAULT is the special name for the default Datasource
static mapping = {
     datasources(['lookup', 'DEFAULT'])
}

Haha, a domain can also configured to use all the DataSources
static mapping = {
     datasource 'ALL'
}

Namespaces and GORM Methods
If a domain class uses more than one DataSource then we can use the namespace implied by each DataSource name to make GORM calls for a particular DataSource.

def zipCode = ZipCode.auditing.get(42)
…snip…
zipCode.auditing.save()

Hibernate Mapped Domain Classes


Services
class DataService {
     static datasource = 'lookup'
     …snip...
}

4.4 Externalized Configuration
In this example, we are losing configuration files (both Java Properties files and ConfigSlurper configurations) from different places on the class path and files located in USER_HOME.
Setting in Config.groovy

grails.config.locations = [
     "classpath:${appName}-config.properties",
     "classpath:${appName}-config.groovy",
     "file:${userHome}/.grails/${appName}-config.properties",
     "file:${userHome}/.grails/${appName}-config.groovy"
]

4.5 Versioning
Versioning Basics
When we create the project, the version is 0.1.
>grails set-version 0.2

Detecting Versions at Runtime
def version = grailsApplication.metadata['app.version']
Within controllers there is an implicit grailsApplication variable that can be used.

def grailsVersion = grailsApplication.metadata['app.grails.version']

alternatively

import grails.util.GrailsUtil

def grailsVersion = GrailsUtil.gailsVersion

4.6 Project Documentation
>grails doc

4.7 Dependency Resolution
grails-app/conf/BuildConfig.groovy

grails.project.dependency.resolution = {
     …
     dependencies {
          runtime 'mysql:mysql-connector-java:5.1.16'
     }
     plugins {
          compile ":hibernate:$grailsVersion"
          compile ":jquery:1.6.1.1"
          compile ":resources:1.0"
         
          build ":tomcat:$grailsVersion"
     }
}

4.7.1 Configurations and Dependencies
Grails features five dependency resolution configurations( or rather say scopes)
build Dependencies for the build system only
compile Dependencies for the compile step
runtime Dependencies needed at runtime but not for compilation
test Dependencies needed for testing but not at runtime
provided Dependencies needed at development time, but not during WAR deployment

Disabling transitive dependency resolution
By default, Grails will not only get the JARs and plugins that you declare, but it will also get their transitive dependencies. We can disable transitive dependency resolution.

runtime('com.mysql:mysql-connector-java:5.1.16',
            'net.sf.ehcache:ehcache:1.6.1') {
     transitive = false
}

Alternatively

runtime group: 'com.mysql',
            name: 'mysql-connector-java',
            version: '5.1.16',
            transitive:false

Excluding specific transitive dependencies
runtime('com.mysql:mysql-connector-java:5.1.16',
           'net.sf.ehcache:ehcache:1.6.1') {
     excludes "xml-apis", "commons-logging"
}

alternatively

runtime(…snip..) {
     excludes([ group: 'xml-apis', name: 'xml-apis'],
                    …
                    [name:'commons-logging' ] )
}

Where are the JARs
The default place is user.home/.grails/ivy-cache, we can change in settings.groovy
grails.dependency.cache.dir = "${userHome}/.my-dependency-cache"


References:
http://grails.org/doc/latest/guide/conf.html
http://grails.org/doc/latest/guide/index.html


分享到:
评论

相关推荐

    the definitive guide to grails 2

    《Grails 2 的终极指南》是一本深入探讨Grails框架精髓的专业书籍,该书以英文撰写,旨在为读者提供全面、深入的Grails框架学习资料。Grails框架基于Groovy语言,是一种高度动态、敏捷的Java应用开发框架,它简化了...

    The definitive guide to grails 2 英文版 书 代码

    《The Definitive Guide to Grails 2》是Grails框架深入学习的重要参考资料,由业界专家撰写,旨在为开发者提供全面、详尽的Grails 2技术指导。这本书结合了理论与实践,不仅介绍了Grails的基本概念,还涵盖了高级...

    grails3 CAS链接接数据库

    标题中的“grails3 CAS链接接数据库”表明我们要讨论的是如何在Grails 3框架下集成CAS(Central Authentication Service)并实现与数据库的连接。Grails是一个基于Groovy语言的开源Web应用框架,而CAS是一种广泛使用...

    grails 开发框架-3

    grails1.0开发框架3 类似于ruby on rails的框架。

    Grails Grails Grails

    4. **命令行工具**:Grails 提供强大的命令行工具,支持创建项目、运行应用、生成代码等任务,大大提升了开发效率。 5. **构建工具**:Grails 使用Gradle作为其构建工具,允许自定义构建流程和依赖管理。 **Grails...

    Grails 3开发邮件发送功能

    Grails 3版本作为该框架的更新迭代产物,同样支持邮件发送服务。 首先,要在Grails 3中开发邮件发送功能,你需要遵循以下步骤: 1. 新建一个Grails项目。在创建项目的过程中,你需要按照Grails框架的约定设置好...

    The definate guide to Grails

    3. **实践 Grails 项目**:通过构建实际的 Web 应用,加深对 Grails 各个特性的理解。 4. **参与社区**:加入 Grails 社区,与其他开发者交流心得,获取最新的技术动态。 《Grails 完全指南》不仅是一本学习资料,...

    Grails权威指南 Grails权威指南

    3. **GORM(Grails Object-Relational Mapping)**:Grails的内置ORM工具,允许开发者以声明式的方式操作数据库,支持SQL的CRUD操作,简化了数据持久化的过程。GORM支持多种数据库,如MySQL、PostgreSQL等。 4. **...

    grails 开发框架-4

    grails1.0开发框架4 类似于ruby on rails的框架。

    Grails3配置单独的数据库properties.pdf

    Grails 3是其一个重要的版本更新,带来了许多改进和新特性。本篇将详细介绍如何在Grails 3中配置单独的数据库properties,以便在开发过程中管理多个数据源,满足复杂的企业级需求。 首先,我们需要理解Grails中的...

    grails-3.0.5-ga (2/2)

    3. **GORM (Grails Object-Relational Mapping)**: GORM是Grails的内置ORM框架,允许开发者使用Groovy DSL来处理数据库操作,支持多种数据库,如MySQL、PostgreSQL等。 4. **Grails Command Line Interface (CLI)**...

    Eclipse下搭建Grails项目

    【Grails项目搭建详解】 Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,尤其适合快速构建动态网站。在Eclipse中搭建Grails项目可能相对复杂,但通过以下步骤,即使是初学者也能顺利进行。 1. *...

    Definitive.Guide.to.Grails

    Definitive Guide to Grails

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    3. Convention over Configuration(CoC):Grails的核心理念之一就是“约定优于配置”,这意味着开发者在很多情况下不需要写大量的配置文件,框架会自动根据约定进行工作。 二、Grails中文文档的价值 1. 学习入口...

    Grails : A Quick-Start Guide

    Using the principle of convention-over-configuration and the dynamic Groovy programming language, Grails takes the pain out of web development and brings back the fun. This book will get you up and ...

    The definitive guide to grails_2 源代码

    《The Definitive Guide to Grails 2》是关于Grails框架的一本权威指南,它为读者提供了深入理解和使用Grails 2开发Web应用程序所需的知识。Grails是一种基于Groovy语言的开源全栈式Web应用框架,它借鉴了Ruby on ...

Global site tag (gtag.js) - Google Analytics