如何扩展用于“grails run-app”的类路径?
我的 Config.groovy 文件中有以下内容:
grails.config.locations = [ "classpath:env.groovy" ]
现在,我应该在哪里放置“env.groovy”,以便在 grails run-app 期间它在 CLASSPATH 上可用?这里的文档非常缺乏。
我可以通过将“env.groovy”放在 $APP_HOME/etc 中然后运行来让它在纯命令行上工作:
$ grails -classpath ./etc run-app
这看起来有点黑客,但我可以忍受它......但是,我无法得到当我使用 Grails eclipse 插件 (STS) 启动 run-app 时,任何此类配置都有效:
Unable to load specified config location classpath:env.groovy : class path resource [env.groovy] cannot be opened because it does not exist
我看过相关帖子 此处,此处,此处,以及 这里 但答案并不令人满意。
我正在寻找一个基于 CLASSPATH 的解决方案,该解决方案将在开发模式下与“run-app”一起使用(命令行和 eclipse)。我知道如何为我的部署 servlet 容器设置 CLASSPATH,所以这不是问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Eric,我们完成此操作的方法是使用配置文件的位置指定 Java 系统属性,然后我们在 Config.groovy 上获取该属性,如下所示:
如您所见,我们仅设置该文件所在的文件夹位于 Java 系统属性内部,按照惯例,文件名应该是应用程序名称+“-config.groovy”,但如果需要,您可以指定整个路径,包括系统属性内的文件名。
然后,在运行应用程序时,您只需像这样设置变量:
正如您在代码中看到的,如果 Java 系统属性变量尚未定义,有一个 if 语句会阻止您查找配置文件,这样您就可以运行您的应用程序,无需使用外部配置文件,只需使用 Config.groovy 中定义的配置设置。
如果您在 Eclipse 或 IntelliJ 中运行应用程序,则可以将此变量作为 JVM 变量传递。
这是一个与必须更改类路径或将配置文件包含在类路径中以便应用程序选择它不同的选项。
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:
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:
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.
我们可以在 _Events.groovy 中添加编译后事件,将外部配置文件复制到类路径,如下所示:
您可以找到更多详细信息 此处
We can add a post compilation event in _Events.groovy to copy our external configuration file to classpath like this:
You can find more details here
项目主目录中应该有一个名为
.classpath
的文件。我不确定,但看看该文件。
There should a file named
.classpath
in project home.I am not sure, but take a look at that file.
类路径中的文件夹在文件 .classpath 中列出
默认情况下,在 grails 2.5.0 中是:
grails-app conf 中的文件将被复制到 WEB-INF/classes 并将成为类路径的一部分
folders in the classpath are listed in the file .classpath
by default in grails 2.5.0 are :
files in grails-app conf will be copied to WEB-INF/classes and will be part of the classpath