【问题标题】:Grails script call my own java classesGrails 脚本调用我自己的 java 类
【发布时间】:2011-10-01 04:35:17
【问题描述】:

我刚刚创建了一个 grails 脚本来创建数据库,但是代码是 Java 的,我想我可以在 grails 脚本中使用它。

这是我的脚本

导入 com.test.database.* 导入 com.test.constant.* 导入 org.neo4j.kernel.* target(main: "这里是脚本的描述!") { db = DataRepository.getInstance(new EmbeddedGraphDatabase( 常量.PROJECT_PATH + "/web-app/WEB-INF/resources/db")) } 设置默认目标(主要)

我的 java 类在 src/java 中,但是当我运行脚本时出现此错误

执行脚本 DbCreate 时出错:没有这样的属性:类的 DataRepository:DbCreate 没有这样的属性:DataRepository 类:DbCreate 在 gant.Gant$_dispatch_closure5.doCall(Gant.groovy:387) 在 gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415) 在 gant.Gant$_dispatch_closure7.doCall(Gant.groovy) 在 gant.Gant.withBuildListeners(Gant.groovy:427) 在 gant.Gant.this$2$withBuildListeners(Gant.groovy) 在 gant.Gant$this$2$withBuildListeners.callCurrent(未知来源) 在 gant.Gant.dispatch(Gant.groovy:415) 在 gant.Gant.this$2$dispatch(Gant.groovy) 在 gant.Gant.invokeMethod(Gant.groovy) 在 gant.Gant.executeTargets(Gant.groovy:590) 在 gant.Gant.executeTargets(Gant.groovy:589) 引起:groovy.lang.MissingPropertyException:没有这样的属性:类的DataRepository:DbCreate 在 DbCreate$_run_closure1.doCall(DbCreate:11) 在 gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)

我的问题是我可以在 grails 脚本中调用用户定义的 java 类吗?

【问题讨论】:

  • 我们可以假设 DataRepository.java 在您列出的导入之一中吗?
  • 我不确定我是否在 Gant 脚本中正确导入它。还有其他方法可以确保它在类路径中吗?

标签: grails gant


【解决方案1】:

如果您使用的是 1.3.6 或更高版本,您可以使用 http://grails.org/doc/latest/ref/Command%20Line/run-script.html,如果您使用的是旧版本的 Grails,您可以从 http://naleid.com/blog/2010/12/03/grails-run-script-updated-for-grails-1-3-5/ 获取脚本

【讨论】:

    【解决方案2】:

    您可以像这样使用 classLoader 直接加载类...

    def dataRepo = classLoader.loadClass('path.to.DataRepository') 
    

    或者你也可以使用...

    includeTargets << grailsScript('_GrailsBootstrap') 
    

    这应该加载你所有的成员。

    【讨论】:

    • 不幸的是,这并没有做一个完整的引导。尝试访问例如org.codehaus.groovy.grails.commons.ConfigurationHolder.config.dataSource.url 来自脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2012-11-15
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多