(Quick Reference)

3 Setting up the configuration - Reference Documentation

Authors: Alejandro GarcĂ­a Granados

Version: 0.4

3 Setting up the configuration

The Optimus plugin lets you generate/overwrite some configuration artifacts. Basically, the plugin generates a config.properties external file, overrides the default LOG4J configuration and modifies the way the application gets the database data connection.

To do this, execute the create-config command:

grails create-config [domainClass]
The domain class name is required. It is used in the grails-app/conf/Config.groovy file generation.
The plugin will generate the following artifacts:
  • src/java/config.properties. Contains the database data connection.
  • grails-app/conf/Config.groovy. It contains the reference to the previous config.properties file and the modified configuration for LOG4J.
  • grails-app/conf/DataSource.groovy. It is similar to the original DataSource.groovy file, but data connection has been changed to the config.properties file

You can generate each file with the following commands:

FileCommand
src/java/config.propertiescreate-properties-file
grails-app/conf/Config.groovycreate-config-file
grails-app/conf/DataSource.groovycreate-datasource-file
These commands will overwrite some important files, such as grails-app/conf/Config.groovy and grails-app/conf/DataSource.groovy. Be careful if you are working on an existing application and you have previosuly customized these files.

src/java/config.properties

The src/java/config.properties file contains something like this:
#Development and test enviroment
dataSource.dbCreate=create-drop
dataSource.driverClassName=org.h2.Driver
dataSource.url=jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000
dataSource.username=sa
dataSource.password=
dataSource.dialect=org.hibernate.dialect.H2Dialect

#Production enviroment (MySQL) #dataSource.driverClassName=com.mysql.jdbc.Driver #dataSource.url=jdbc:mysql://localhost:3306/mydb #dataSource.username=username #dataSource.password=password #dataSource.dialect=org.hibernate.dialect.MySQLDialect

#Production enviroment (PostgreSQL) #dataSource.driverClassName=org.postgresql.Driver #dataSource.url=jdbc:postgresql://localhost:5432/mydb #dataSource.username=username #dataSource.password=password #dataSource.dialect=org.hibernate.dialect.PostgreSQLDialect

All the properties that you would set in the grails-app/conf/DataSource.groovy file has been externalized to this file. When you deploy the application, you can change these values without recompile the entire application. You just have to restart it.

grails-app/conf/Config.groovy

This files remains pretty equal to the original one generated by Grails. The things the plugin changes are the configuration of the src/java/config.properties file:
grails.config.locations = [ "classpath:config.properties" ]
Besides, the default configuration for LOG4J has been changed:
import org.apache.log4j.rolling.RollingFileAppender
import org.apache.log4j.rolling.TimeBasedRollingPolicy
import org.apache.log4j.EnhancedPatternLayout
…
// packages to include in Spring bean scanning
grails.spring.bean.packages = [ 'mypackage.aop' ]
…
log4j = {

def layout = new EnhancedPatternLayout() layout.conversionPattern = '%d{dd-MM-yyyy HH:mm:ss.SSS} [%t] %-5p %c{1} - %m%n' def rollingFile = new RollingFileAppender() rollingFile.name = 'rollingFileAppender' rollingFile.layout = layout def rollingPolicy = new TimeBasedRollingPolicy() rollingPolicy.fileNamePattern = 'log/activity.%d.zip' rollingPolicy.activeFileName = 'log/activity' rollingPolicy.activateOptions() rollingFile.rollingPolicy = rollingPolicy

appenders { appender rollingFile 'null' name:'stacktrace' }// End of closure

error rollingFileAppender:[ 'org.codehaus.groovy.grails.web.servlet', // controllers 'org.codehaus.groovy.grails.web.pages', // GSP 'org.codehaus.groovy.grails.web.sitemesh', // layouts 'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping 'org.codehaus.groovy.grails.web.mapping', // URL mapping 'org.codehaus.groovy.grails.commons', // core / classloading 'org.codehaus.groovy.grails.plugins', // plugins 'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration 'org.springframework', 'org.hibernate', 'net.sf.ehcache.hibernate' ]

info rollingFileAppender:[ 'mypackage.aop' ]

}// End of closure

With this configuration, you can see the logs in the log directory and in the console output at the same time. You can also notice that the logs are zipped and stored every day.

Finally, some properties have been added:

grails {
  optimus {
    tab = 4
    blockComments = false
  }
}

grails-app/conf/DataSource.groovy

The data connection has been removed completely from this file and now it appears in the src/java/config.properties file:
dataSource {
    url = ""
    driverClassName = ""
    username = ""
    password = ""
    dialect = ""
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = ""
        }
    }
    test {
        dataSource {
            dbCreate = ""
        }
    }
    production {
        dataSource {
            pooled = true
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=true
               validationQuery="SELECT 1"
            }
        }
    }
}