5

I am trying to create a simple Grails 3 project and got stuck with something really simple. So I want my data source properties to come from VM options that I set in my IntelliJ IDE. Before in Grails 2.x, I just used to do something like:

environments {
    development{
        //Database connection properties
        def dbserver = System.properties.getProperty('dbserver')
        def dbport = System.properties.getProperty('dbport')
        ............
        dataSource {
            url: "jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname}
        } 
    }

Now that I have application.yml, how do I access "System.properties" and embed it in yml? I have read that we can instead use application.groovy if YML doesn't support it, in that case this is what the application.groovy looks like :

grails {
    profile = 'web'
    codegen {
        defaultPackage = 'defPack'
    }
}

info {
    app {
        name = '@info.app.name@'
        version = '@info.app.version@'
        grailsVersion = '@info.app.grailsVersion@'
    }
}

spring {
    groovy {
        template['check-template-location'] = false
    }
}

hibernate {
    naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy'
    cache {
        queries = false
    }
}

grails {
    mime {
        disable {
            accept {
                header {
                    userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident']
                }
            }
        }

        types {
            all = '*/*'
            atom = 'application/atom+xml'
            css = 'text/css'
            csv = 'text/csv'
            form = 'application/x-www-form-urlencoded'
            html = ['text/html', 'application/xhtml+xml']
            js = 'text/javascript'
            json = ['application/json', 'text/json']
            multipartForm = 'multipart/form-data'
            rss = 'application/rss+xml'
            text = 'text/plain'
            hal = ['application/hal+json', 'application/hal+xml']
            xml = ['text/xml', 'application/xml']
        }
    }
    urlmapping {
        cache {
            maxsize = 1000
        }
    }
    controllers {
        defaultScope = 'singleton'
    }
    converters {
        encoding = 'UTF-8'
    }
    views {
        default { codec = 'html' }
        gsp {
            encoding = 'UTF-8'
            htmlcodec = 'xml'
            codecs {
                expression = 'html'
                scriptlets = 'html'
                taglib = 'none'
                staticparts = 'none'
            }
        }
    }
}
dataSource {
    pooled = true
    jmxExport = true
    driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
    dbCreate = ''
    username = 'someUsername'
    password = 'somePass'
}
environments {
    development {
        dataSource {
            url = 'jdbc:sqlserver://localhost:1234;databaseName=someDbName;'
        }
    }
}

Thanks.

UPDATE:

application.groovy is not being taken in by default, even when I removed application.yml

Deewendra Shrestha
  • 2,054
  • 1
  • 20
  • 47

2 Answers2

4

Turned out I had an issue, I needed to put 'default' keyword within quotes. Like :

grails {
    profile = 'web'
    codegen {
        defaultPackage = 'defPack'
    }
}

info {
    app {
        name = '@info.app.name@'
        version = '@info.app.version@'
        grailsVersion = '@info.app.grailsVersion@'
    }
}

spring {
    groovy {
        template['check-template-location'] = false
    }
}

hibernate {
    naming_strategy = 'org.hibernate.cfg.DefaultNamingStrategy'
    cache {
        queries = false
    }
}

grails {
    mime {
        disable {
            accept {
                header {
                    userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident']
                }
            }
        }

        types {
            all = '*/*'
            atom = 'application/atom+xml'
            css = 'text/css'
            csv = 'text/csv'
            form = 'application/x-www-form-urlencoded'
            html = ['text/html', 'application/xhtml+xml']
            js = 'text/javascript'
            json = ['application/json', 'text/json']
            multipartForm = 'multipart/form-data'
            rss = 'application/rss+xml'
            text = 'text/plain'
            hal = ['application/hal+json', 'application/hal+xml']
            xml = ['text/xml', 'application/xml']
        }
    }
    urlmapping {
        cache {
            maxsize = 1000
        }
    }
    controllers {
        defaultScope = 'singleton'
    }
    converters {
        encoding = 'UTF-8'
    }
    views {
        'default' { codec = 'html' }//THIS WAS THE SOURCE OF ERROR
        gsp {
            encoding = 'UTF-8'
            htmlcodec = 'xml'
            codecs {
                expression = 'html'
                scriptlets = 'html'
                taglib = 'none'
                staticparts = 'none'
            }
        }
    }
}

def dbserver = System.properties.getProperty('dbserver')
def dbport = System.properties.getProperty('dbport')
def dbusername = System.properties.getProperty('dbusername')
def dbpassword = System.properties.getProperty('dbpassword')
def dbname = System.properties.getProperty('dbname')

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
    dbCreate = ''
    username = dbusername
    password = dbpassword
}
environments {
    development {
        dataSource {
            url = 'jdbc:sqlserver://${dbserver}:${dbport};databaseName=${dbname}'
        }
    }
}
Deewendra Shrestha
  • 2,054
  • 1
  • 20
  • 47
  • how did you added application.groovy file? meaning you made it your self.. ? as in your update you said it does not take by default!! And than in application.groovy file is it possible access "System.properties" and other Java/Groovy related object. FYI I need to use my own created groovy class objects as well [Need to import classes]. Can you please point out some solution upon this! – emphywork Nov 11 '16 at 12:25
  • 1
    Yes, you would have to add it manually and then get rid of your application.yml file, or else I think the precedence is on yml version and not the groovy one. Yes, in groovy file you can use any System.properties, I have not tried importing classes but you can give "import" a try and see what happens. – Deewendra Shrestha Nov 11 '16 at 16:03
1

May be not a direct answer to your question. You can access system properties in application.yml by adding below to build.gradle

tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperties = System.properties

Reference: https://github.com/grails/grails-core/issues/9086

Gandhi
  • 11
  • 2