1

我刚刚创建了一个 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 类吗?

4

2 回答 2

1

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

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

或者你也可以使用...

includeTargets << grailsScript('_GrailsBootstrap') 

哪个应该加载所有成员。

于 2011-07-08T12:56:07.357 回答
0

如果您使用的是 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/的脚本

于 2011-07-08T12:58:02.147 回答