2

I need to override grails.serverURL at runtime without having to regenerate the application's WAR file. I have tried various ways of setting grails.serverURL in the application.properties file and cannot get it to work.

Here is the environment specific portion of Config.groovy:

environments {
   prod
   {
      grails.serverURL = "http://nonexistentserver.somecompany.com:8080"
      grails.anotherappspecificURL = "xcc://user:password@server.somecompany.com"
   }

Basically, our application.properties looks like this:

grails.env=prod
grails.war.deployed=true
app.grails.version=1.0.4
app.name=myapp

Below is one of the ways I have tried to override the settings. These are defined in Config.groovy:

grails.serverURL=http://webserver1.somecompany.com:8080
grails.anotherappspecificURL=xcc://admin:xyzabc123@specificserver.somecompany.com

Any help with getting this to work without having to make code changes would be greatly appreciated!

cdeszaq
  • 30,869
  • 25
  • 117
  • 173

3 Answers3

3

The proper way to override values in Config.groovy is to use an external properties file, see:

http://grails.org/doc/1.1.x/guide/3.%20Configuration.html#3.4%20Externalized%20Configuration

Specify an external properties file in Config.groovy, for example:

grails.config.locations = [ "classpath:app-config.properties"]

In the properties file (can be stored in grails-app/conf/) specify the override value:

grails.serverURL=http://webserver1.somecompany.com:8080

Anytime you need to change the serverURL once the war is deployed just modify the properties file in /WEB-INF/classes/app-config.properties and reload the context.

John Wagenleitner
  • 10,967
  • 1
  • 40
  • 39
  • Yup, I got that from the documentation. Problem is, it isn't working. Our Config.groovy doesn't have anything set for grails.config.location (it's commented out), so I assume that the default external properties file is "application.properties". It appears that is the case since I can change the setting for "grails.env" and it picks up the proper "environment" configuration that was defined in Config.groovy. However, I want to override those settings with something else at runtime startup and doing as you noted doesn't have any affect on any of the grails. settings. –  Jun 25 '09 at 19:51
  • To clarify my original post, the last "code" section is in the application.properties file, not the Config.groovy file, as the comment before that code section might suggest. –  Jun 25 '09 at 19:58
  • I believe the application.properties contains application meta data that is not merged in with the configuration data contained in Config.groovy. Therefore, in order to override a parameter that would normally be set in Config.groovy you need to use grails.config.location and not application.properties. – John Wagenleitner Jun 25 '09 at 20:16
  • I understood your last code section was from application.properties and the point of my answer was to say that is not the correct place (i.e., wont work) to override config values. – John Wagenleitner Jun 25 '09 at 20:20
0

This may not be relevant, but I noticed you're missing quotation marks in your grails.serverURL

mikermcneil
  • 11,141
  • 5
  • 43
  • 70
0

I've found that externalized configuration is a little tricky (as of Grails 1.3.7). You have to put your file into grails.config.locations in Config.groovy

grails.config.locations << 'classpath:my-config-file.groovy'

But you can't access the properties without adding another file. I've made it work by putting a new configuration file into grails-app/conf and adding it to the classpath by adding the following to scripts/Events.groovy.

eventCompileEnd = {
    ant.copy(todir:classesDirPath) {
      fileset(file:"${basedir}/grails-app/conf/SecurityConfig.groovy")
    }
}

You can find more information at https://stackoverflow.com/a/9789506/1269312

Community
  • 1
  • 1
Big Ed
  • 1,056
  • 9
  • 20