我刚刚创建了grails脚本来创建数据库,但是代码是用Java编写的,我认为我可以在grails脚本中使用它。
这是我的剧本
导入com.test.database。*
导入com.test.constant。*
导入org.neo4j.kernel。*
target(main:“脚本的描述在这里!”){
db = DataRepository.getInstance(new EmbeddedGraphDatabase(
Constant.PROJECT_PATH +“/ web-app / WEB-INF / resources / db”))
}
setDefaultTarget(main)
我的Java类位于src / java中,但是当我运行脚本时遇到了此错误
执行脚本DbCreate时出错:无此类属性:类DbCreate的DataRepository
没有这样的属性:类DbCreate的DataRepository
在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)
at 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:无此类属性:类DbCreate的DataRepository
在DbCreate $ _run_closure1.doCall(DbCreate:11)
在gant.Gant $ _dispatch_closure5.doCall(Gant.groovy:381)
我的问题是我可以在grails脚本中调用用户定义的Java类吗?
最佳答案
您可以像这样使用classLoader直接加载类...
def dataRepo = classLoader.loadClass('path.to.DataRepository')
或者您也可以使用...
includeTargets << grailsScript('_GrailsBootstrap')
应该加载所有成员。
关于grails - Grails脚本调用我自己的Java类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6623394/