grails - Grails脚本调用我自己的Java类

标签 grails gant

我刚刚创建了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/

相关文章:

grails - 如何在grails中进行网络搜索

grails - GORM 标准查询 : Finding Children with specific properties

grails - spring-security-rest 和 guava 之间的间接 jar 冲突导致 NoSuchMethod 错误

grails - 如何创建用于代码生成的 CLI 脚本,例如 "grails create-controller"所做的

grails - Gradle 与 Gant

grails - 如何在 Grails 3.x 中配置 JNDI 数据源?

grails - 限制为大写的Groovy字符串

grails - Gant,Groovy SCP和Ant

grails - 在脚本中使用 Grails 服务

grails - 在Grails的Gant任务中使用插件