0

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.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
carl-lopez
  • 584
  • 5
  • 12
  • 2
    Are you definitely sure that those environment variables are not being passed _to the relevant process on the server_ (Tomcat?) even if they're not set in whatever shell you checked? Usually when I need to set things like this I put the environment settings into the Tomcat start up script. – Ian Roberts Feb 15 '14 at 09:08
  • That's what I was missing Ian, they were being set in the script. Thank you! – carl-lopez Feb 15 '14 at 14:31

3 Answers3

1

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?

mikemil
  • 1,213
  • 10
  • 21
1
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()
}
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

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.

Aleksander Rezen
  • 877
  • 7
  • 14