carl-lopez
carl-lopez

Reputation: 582

Grails external configuration file

I started working with an existing Grails project and this is the part of the config file (Config.groovy) that deals with external configuration files:

grails.config.locations = [];

def defaultConfigFile = "${basedir}/configs/BombayConfig.groovy"
def defaultDatasourceFile = "${basedir}/configs/BombayDataSource.groovy"

if(File.separator == "\\") {
    defaultConfigFile = defaultConfigFile.replace('/', '\\')
    defaultDatasourceFile = defaultDatasourceFile.replace('/', '\\')
}

String PROJECT_NAME = "BOMBAY"
String CONFIG_ENV = "${PROJECT_NAME}_CONFIG_LOCATION"
String DATASOURCE_ENV = "${PROJECT_NAME}_DATASOURCE_LOCATION"

def externalConfig = System.getenv(CONFIG_ENV)
def externalDataSource = System.getenv(DATASOURCE_ENV)

if (externalConfig && new File(externalConfig).isFile()) {
    grails.config.locations << "file:" + externalConfig
}
else {
    grails.config.locations << "file:" + defaultConfigFile
}

if (externalDataSource && new File(externalDataSource).isFile()) {
    grails.config.locations << "file:" + externalDataSource
}
else {
    grails.config.locations << "file:" + defaultDatasourceFile
}

I have some default files in configs folder, but in the server the files that are being used reside in:

/home/someusername/configuration/

which doesn't look like the default path, and there are no environment variables pointing to that path as the configuration suggests.

I also tried to look for a configs folder linked to the other configuration folder, but didn't find anything.

I'm lost here; how else can configuration files be specified to Grails?

Edit: To clarify on things, the server is running and works, I just want to figure out how it's picking up the configuration files from the above specified path.

Upvotes: 0

Views: 5636

Answers (3)

Aleksander Rezen
Aleksander Rezen

Reputation: 927

In case you are struggling with external configs and you have specifies external configs like so in Config.groovy

grails.config.locations = []

if(System.properties["${appName}.config.location"]) {
    grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}

if(System.properties["${appName}.datasource.config.location"]) {
    grails.config.locations << "file:" + System.properties["${appName}.datasource.config.location"]
}

and your set environment is something like this

export GRAILS_OPTS="$GRAILS_OPTS -DAppName.config.location=dev-config/Config.groovy"
export GRAILS_OPTS="$GRAILS_OPTS -DAppName.datasource.config.location=dev-config/DataSource.groovy"

and dev configs are not being picked up, then you need to look into your fork settings in BuildConfig.groovy and turn them off like so

grails.project.fork = [
  test: false,
  run: false, 
]

This way, when application is forked, your system properties will not be lost.

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122414

if(File.separator == "\\") {
    defaultConfigFile = defaultConfigFile.replace('/', '\\')
    defaultDatasourceFile = defaultDatasourceFile.replace('/', '\\')
}

This will probably cause problems - grails.config.locations is a list of URLs, not native file paths, so you definitely do want forward, not backward slashes. Instead try something like

File defaultConfigFile = new File(basedir, "configs/BombayConfig.groovy")
// and the same for datasource

// ...
File externalConfig = System.getenv(CONFIG_ENV) ? new File(System.getenv(CONFIG_ENV)) : null

if (externalConfig?.isFile()) {
    grails.config.locations << externalConfig.toURI().toString()
}
else {
    grails.config.locations << defaultConfigFile.toURI().toString()
}

Upvotes: 1

mikemil
mikemil

Reputation: 1251

I think the intention is that you use the grails.config.locations[] entry. The following is commented out in my Config.groovy

// grails.config.locations = [ "classpath:${appName}-config.properties",    
//                             "classpath:${appName}-config.groovy",
//                             "file:${userHome}/.grails/${appName}-config.properties",
//                             "file:${userHome}/.grails/${appName}-config.groovy"]

So can't you specify something like this:

grails.config.locations = [ "file:${userHome}/configuration" ] 

to pickup the files in that folder?

Upvotes: 1

Related Questions