How to override grails configuration from command line

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

 grails <set property> dbm-status 

My first attempt was to use the -D command-line option as

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

I tried adding an additional configuration file to grails.config.locations , which gets the values ​​from System.getProperty , but it doesn't seem to work.

There is a built-in way to override configuration values ​​from the command line, otherwise how can I enter a parameter from the command line into the grails configuration?

EDIT: I do not want to use another environment / data source to avoid duplicating the data source configuration and the need to configure things for this new environment.

+5
source share
1 answer

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... ... 
+1
source

Source: https://habr.com/ru/post/1212113/


All Articles