13

我的 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 的解决方案,该解决方案将在开发模式下与“运行应用程序”一起使用(命令行和来自 Eclipse)。我知道如何为我的部署 servlet 容器设置 CLASSPATH,所以这不是问题。

4

4 回答 4

6

Eric,我们这样做的方法是通过使用配置文件的位置指定一个 Java 系统属性,然后我们在 Config.groovy 中获取它,如下所示:

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

如您所见,我们仅设置文件在 Java 系统属性内的文件夹,按照惯例,我们说文件名应该是应用程序名称 +“-config.groovy”,但如果需要,您可以指定整个路径,包括系统属性中的文件名。

然后在运行应用程序时,您只需像这样设置变量:

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

正如您在代码中看到的那样,如果尚未定义 Java 系统属性变量,则 if 语句会阻止您查找配置文件,这样您就可以在不使用外部配置文件的情况下运行您的应用程序,而只需使用Config.groovy 中定义的配置设置。

如果您在 Eclipse 或 IntelliJ 中运行应用程序,则将此变量作为 JVM 变量传递。

这是一个不同于必须更改类路径或在类路径中包含配置文件以便应用程序选择它的选项。

于 2011-03-08T00:47:29.450 回答
3

我们可以在 _Events.groovy 中添加一个编译后事件来将我们的外部配置文件复制到类路径中,如下所示:

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

您可以在此处找到更多详细信息

于 2012-02-20T12:58:28.063 回答
0

.classpath项目主页中应该有一个名为的文件。
我不确定,但看看那个文件。

于 2011-03-05T17:34:05.950 回答
0

在 grails 2.5.0 中,类路径中的文件夹默认列在文件 .classpath 中:

<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"/>

grails-app conf 中的文件将被复制到 WEB-INF/classes 并成为类路径的一部分

于 2015-08-30T13:11:17.187 回答