13

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.

Community
  • 1
  • 1
Eric
  • 836
  • 1
  • 11
  • 18
  • excuse me, but I really don't understand why you want this work? Why not simply use Config.groovy for development instead? – Hoàng Long Mar 06 '11 at 18:42
  • 1
    Because Config.groovy is checked in to source control. I want a file that I can put environment-specific properties in (individual developer-specific database connection strings, etc.) that will **not** be checked in to source control. – Eric Mar 08 '11 at 00:32
  • Good idea.I personally have some kind of trouble with the conflict when committing that file. – Hoàng Long Mar 08 '11 at 02:01

4 Answers4

6

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.

Maricel
  • 2,089
  • 13
  • 17
  • Thanks Maricel - I saw that example commented out in the default Config.groovy created by Grails. This approach can work, but as a long time Java programmer, using "file:"-based resource lookups instead of "classpath:"-based resource lookups has a "bad smell". Perhaps I'll just have to live with it though... I would think there has to be a classpath-based approach that works here. – Eric Mar 09 '11 at 02:08
3

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

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Muein Muzamil
  • 3,388
  • 2
  • 13
  • 3
  • This actually copies the file to class path, so it affects prod env / war too. what if we just want to modify just run-app or test-app class path – Sudhir N Sep 25 '15 at 14:23
0

folders in the classpath are listed in the file .classpath by default in grails 2.5.0 are :

<classpathentry kind="src" path="src/java"/>
<classpathentry kind="src" path="src/groovy"/>
<classpathentry excluding="spring/" kind="src" path="grails-app/conf"/>
<classpathentry kind="src" path="grails-app/conf/spring"/>
<classpathentry kind="src" path="grails-app/controllers"/>
<classpathentry kind="src" path="grails-app/domain"/>
<classpathentry kind="src" path="grails-app/i18n"/>
<classpathentry kind="src" path="grails-app/services"/>
<classpathentry kind="src" path="grails-app/taglib"/>
<classpathentry kind="src" path="grails-app/utils"/>
<classpathentry kind="src" path="grails-app/views"/>
<classpathentry kind="src" path="test/integration"/>
<classpathentry kind="src" path="test/unit"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.grails.ide.eclipse.core.CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/eclipseclasses"/>

files in grails-app conf will be copied to WEB-INF/classes and will be part of the classpath

Toumi
  • 2,925
  • 4
  • 37
  • 31
0

There should a file named .classpath in project home.
I am not sure, but take a look at that file.

mj.scintilla
  • 638
  • 1
  • 9
  • 20
  • I'm aware of Eclipse's `.classpath file`. However, the eclipse classpath does not correspond to the classpath that grails uses when it launches 'run-app'. – Eric Mar 05 '11 at 18:45
  • Well, I was talking about the grails project `.class`. I have `.class` file in every grails project and I don't use eclipse. This one is grails project `.class` file, not of eclipse. – mj.scintilla Mar 05 '11 at 21:55