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


你可能感兴趣的:(configuration)