Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How override grails configuration from command line

I am trying to override dataSource.url value running grails from the command line, example

grails <set property> dbm-status

My first try was using -D command line parameter as

grails -DdataSource.url=jdbc:sqlserver://xx.xx.xx.xx;databaseName=db_name

I have tried to add an additional config file to grails.config.locations that get values from System.getProperty but does not seems to work.

Exist a built in way to override config values from the command line, otherwise how I can inject parameter from the command line to the grails configuration ?

EDIT: I don't want to use another environment/datasource to avoid datasource configuration duplication and the need to configure things for this new environment.

like image 393
carlo-colombo Avatar asked Jan 28 '15 15:01

carlo-colombo


1 Answers

By including the following if in DataSource.groovy I'm able to override url,password and username property if url is provided. (Valid for Grails 2.x)

....
environments {
    development {
        dataSource {
        url = "jdbc:postgresql://localhost/db"
        username = "user"
        password = "pass"
        if (System.properties['dataSourceUrl']) {
            println 'Taking dataSource url, password, username from command line overrides'
            url = System.properties['dataSourceUrl']
            password = System.properties['dataSourcePassword']
            username = System.properties['dataSourceUsername']
        }
    }
}
...

Now when I run the command, the overrides get applied:

grails dev -DdataSourceUrl=newUrl -DdataSourcePassword=newPass -DdataSourceUsername=newUser run-app

Unfortunately if you want to be able to override on every environment you have to duplicate this code for every env block. If you pull it up to root it won't work since config merging kicks in and the last run will actually apply what's in the env {} block and not what's in the System properties.

Looking at it again something like that looks even better:

...
   url = System.properties['dataSourceUrl'] ?: 'jdbc:postgresql://localhost/db'
   //and for every property...
...
like image 151
Kuba Avatar answered Oct 13 '22 06:10

Kuba