Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend the classpath used for 'grails run-app'

I have the following in my Config.groovy file:

grails.config.locations = [ "classpath:env.groovy" ]

Now, where exactly am I supposed to place "env.groovy" such that it is available on the CLASSPATH during grails run-app? The documentation here is sorely lacking.

I am able to get it to work on the pure commandline by placing "env.groovy" in $APP_HOME/etc and then running:

$ grails -classpath ./etc run-app

This seems a little hackish, but I can live with it... However, I am unable to get any such configuration working when I launch run-app using the Grails eclipse plugin (STS):

Unable to load specified config location classpath:env.groovy : class path resource [env.groovy] cannot be opened because it does not exist

I've seen related posts here, here, here, and here but the answers have been unfulfilling.

I am looking for a CLASSPATH-based solution that will work with 'run-app' in development mode (both commandline and from eclipse). I know how to set up the CLASSPATH for my deployment servlet container, so that is not an issue.

like image 771
Eric Avatar asked Mar 05 '11 15:03

Eric


2 Answers

Eric, the way we have done this is by specifying a Java system property with the location of the config file and then we grab that on the Config.groovy, something like this:

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

As you can see we are setting only the folder where the file is inside the Java system property and by convention we are saying that the file name should be the application name + "-config.groovy", but if you need to you can specify the whole path including the file name inside the system property.

Then when running the application you just set the variable like this:

grails -Dapplication.config.location=/Users/eric/ run-app

As you can see in the code there is an if statement that prevents your from looking for the config file if the Java system property variable has not been defined, in this way you can run your app without using an external config file and just using the config settings defined in Config.groovy.

If you are running your app in Eclipse or IntelliJ you pass this variable as a JVM variable.

This is a different option from having to change the classpath or include the config file in the classpath so the app picks it up.

like image 165
Maricel Avatar answered Oct 01 '22 21:10

Maricel


We can add a post compilation event in _Events.groovy to copy our external configuration file to classpath like this:

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

You can find more details here

like image 20
Muein Muzamil Avatar answered Oct 01 '22 20:10

Muein Muzamil