Wednesday, October 19, 2016

Part 2: Configuration

PART 2: CONFIGURATION

A. DataSource.groovy

Unaltered in the VRS case study.

The dbCreate property of the DataSource definition is important as it dictates what Grails should do at runtime with regards to automatically generating the database tables from GORM classes. The options are described in the DataSource section:
  • create
  • create-drop
  • update
  • validate
  • no value

B. Introduction to BuildConfig.groovy and Config.groovy

BuildConfig.groovy, is for settings that are used when running Grails commands, such as compile, doc, etc. The second file, Config.groovy, is for settings that are used when your application is running. This means that Config.groovy is packaged with your application, but BuildConfig.groovy is not.

When multiple settings have the same base, nesting, or partial nesting can be used:

app {  
  email {
      host = 'mail.njit.edu'      desc = desc
      addr = 'vrs@njit.edu'      err {
         act = false         to = usrs.dev
         addr = 'Bugs <fix-it@njit.edu>'//       view = '/error'      }
   }
}

Accessing settings from your own application:

1. The settings in BuildConfig.groovy are only available from command scripts and can be accessed via the grailsSettings.config property like so:
target(default: "Example command") {
   def maxIterations = grailsSettings.config.myapp.iterations.max    …}


2. For those defined in Config.groovy, use the grailsApplication object, which is available as a variable in controllers and tag libraries:

class MyController {

def hello() {
        def recipient = grailsApplication.config.app.email.err.to
       render "Hello ${recipient}"
   }
}
C. Config.groovy
1. Externalised Configuration

  • grails.config.locations - The location of properties files or addition Grails Config files that should be merged with main configuration.    e.g. 

grails.config.locations = ["file:${app.externPath}"]

where app.externPath is defined as:
externPath = (!local) ? app_extern_config_path : 'target/extern/my_config.groovy'

2. Other Runtime Settings

grails.serverURL - A string specifying the server URL portion of absolute links, including server name e.g. grails.serverURL="http://my.yourportal.com"

D. Per Environment Configuration

Grails supports the concept of per environment configuration. The Config.groovyDataSource.groovy, and BootStrap.groovy files in the grails-app/conf directory can use per-environment configuration using the syntax provided by ConfigSlurper. eg.:

environments {
   test {
      app.email.err.act = true      app.dataSrc.logSql = false   }
   production {
      app.cas.url = 'https://webauth.njit.edu/idp/profile/cas'      .
      .
      app.dataSrc.action = 'update'      app.dataSrc.logSql = false   }
}

There are 3 preset environments - devprod, and test for developmentproduction and test. For example to create a WAR for the test environment you wound run:
grails test war




No comments:

Post a Comment