Showing posts with label BuildConfig.groovy. Show all posts
Showing posts with label BuildConfig.groovy. Show all posts

Thursday, September 19, 2013

Grails access something.properties settings under conf directory in BuildConfig.groovy

Create a file named 'config.properties' under conf direcotry with the following properties:

dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.username = root
tomcatVersion = 2.2.4 

Write the following code in 'BuildConfig.groovy' file:


def props = new Properties();
new File("grails-app/conf/config.properties").withReader {
    props.load(it);
}
def conf = new ConfigSlurper().parse(props);
println conf; 
println conf.dataSource.driverClassName;
println conf.dataSource.username;
println conf.tomcatVeriosn; 

And output will be as:


[dataSource:[driverClassName:com.mysql.jdbc.Driver, username:root], tomcatVersion: 2.2.4]
com.mysql.jdbc.Driver
root
2.2.4 

Then you can use this settings in your 'BuildConfig.groovy' files like this (highlighted yellow):


plugins {
    runtime ":hibernate:2.2.4"
    runtime ":jquery:1.8.3"
    runtime ":resources:1.2"

    // Uncomment these (or add new ones) to enable additional resources capabilities
    //runtime ":zipped-resources:1.0"
    //runtime ":cached-resources:1.0"
    //runtime ":yui-minify-resources:0.1.5"

    build ":tomcat:"+conf.tomcatVersion 
    /* OR USE */
    build ":tomcat:${conf.tomcatVersion}"
 
    runtime ":database-migration:1.3.2" 
    compile ':cache:1.0.1'
}

Grails Access Config.groovy settings in BuildConfig.groovy

Include the following lines in 'BuildConfig.groovy' file


def directory = new File(getClass().protectionDomain.codeSource.location.path).parent;
def config = new ConfigSlurper(grailsSettings.grailsEnv).parse(new File(directory + File.separator + "Config.groovy").toURI().toURL())
println "| Tomcat Version (API): " + config.grails.tomcat.version;

Include the following lines in 'Config.groovy'

grails.tomcat.version = "2.2.4"

Then when run the program it prints like:

| Tomcat Version (API): 2.2.4

Then you can use this settings in your 'BuildConfig.groovy' files like this (highlighted yellow):
plugins {
    runtime ":hibernate:2.2.4"
    runtime ":jquery:1.8.3"
    runtime ":resources:1.2"

    // Uncomment these (or add new ones) to enable additional resources capabilities
    //runtime ":zipped-resources:1.0"
    //runtime ":cached-resources:1.0"
    //runtime ":yui-minify-resources:0.1.5"

    build ":tomcat:"+config.grails.tomcat.version

    runtime ":database-migration:1.3.2"

    compile ':cache:1.0.1'
}