Grails脚本调用我自己的java类

时间:2011-07-08 10:47:35

标签: grails gant

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

这是我的剧本

import com.test.database.*
import com.test.constant.*
import org.neo4j.kernel.*

target(main: "The description of the script goes here!") {
    db = DataRepository.getInstance(new EmbeddedGraphDatabase(
        Constant.PROJECT_PATH + "/web-app/WEB-INF/resources/db"))

}

setDefaultTarget(main)

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

Error executing script DbCreate: No such property: DataRepository for class: DbCreate
No such property: DataRepository for class: DbCreate
    at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:387)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
    at gant.Gant.withBuildListeners(Gant.groovy:427)
    at gant.Gant.this$2$withBuildListeners(Gant.groovy)
    at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
    at gant.Gant.dispatch(Gant.groovy:415)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.executeTargets(Gant.groovy:590)
    at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: groovy.lang.MissingPropertyException: No such property: DataRepository for class: DbCreate
    at DbCreate$_run_closure1.doCall(DbCreate:11)
    at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)

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

2 个答案:

答案 0 :(得分:0)

你可以直接使用classLoader加载这个类......

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

或者您也可以使用......

includeTargets << grailsScript('_GrailsBootstrap') 

哪个应加载所有成员。

答案 1 :(得分: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/获取脚本

相关问题