By including the following if in DataSource.groovy , I can override the url, password, and username property if url is specified. (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, overrides are applied:
grails dev -DdataSourceUrl=newUrl -DdataSourcePassword=newPass -DdataSourceUsername=newUser run-app
Unfortunately, if you want to be able to override in each environment, you have to duplicate this code for each env block. If you pull it by the root, this will not work, as the configuration merges, and the last run will actually apply what is in the env {} block, and not in the system properties.
Looking at it again, something like this looks even better:
... url = System.properties['dataSourceUrl'] ?: 'jdbc:postgresql://localhost/db' //and for every property... ...
source share