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

4

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.

Firepower answered 5/3, 2011 at 15:19 Comment(3)
excuse me, but I really don't understand why you want this work? Why not simply use Config.groovy for development instead?Cinema
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.Firepower
Good idea.I personally have some kind of trouble with the conflict when committing that file.Cinema
W
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.

Wale answered 8/3, 2011 at 0:47 Comment(1)
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.Firepower
H
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

Huneycutt answered 20/2, 2012 at 12:58 Comment(1)
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 pathDrillmaster
C
0

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

Cotsen answered 5/3, 2011 at 17:34 Comment(2)
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'.Firepower
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.Cotsen
S
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

Scholasticate answered 30/8, 2015 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.