开发者

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

开发者 https://www.devze.com 2023-02-15 04:16 出处:网络
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 CLASS

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 deploy开发者_Go百科ment servlet container, so that is not an issue.


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消