carlo-colombo
carlo-colombo

Reputation: 136

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.

Upvotes: 6

Views: 1614

Answers (3)

Main Pal
Main Pal

Reputation: 451

My case: Grails 4.0.3, uses application.yml for configuration.

Command to run:

grails -DdataSourceUrl="jdbc:sqlserver://xx.xx.xx.xx;databaseName=db_name"

Updated build.gradle (bootRun task and added this line):

// value for this property is being set in task: setDataSourceEnv
systemProperty 'dataSourceUrl', System.getProperty('dataSourceUrl')

The resulting task code would look like this:

bootRun {
    ignoreExitValue true
    jvmArgs(
            '-Dspring.output.ansi.enabled=always',
            '-noverify',
            '-XX:TieredStopAtLevel=1',
            '-Xmx2048m')
    sourceResources sourceSets.main
    String springProfilesActive = 'spring.profiles.active'
    systemProperty springProfilesActive, System.getProperty(springProfilesActive)
    // value for this property is being set in task: setDataSourceEnv
    systemProperty 'dataSourceUrl', System.getProperty('dataSourceUrl')
}

Updated grails-app/conf/application.yml to use the variable dataSourceUrl (it can also have dots if you prefer):

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: "${dataSourceUrl}"

I got the idea of using build.gradle looking at the Question itself here

Hope it helps. Happy coding!!!

Upvotes: 0

xpusostomos
xpusostomos

Reputation: 1657

DATASOURCE_URL=jdbc:sqlserver://xx.xx.xx.xx;databaseName=db_name grials run-app

For any variable you want to set, you can set it in the environment. Change to upper case and replace dots with underscores. This is a feature of spring boot.

Upvotes: 0

Kuba
Kuba

Reputation: 936

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...
...

Upvotes: 2

Related Questions