用于填充数据库的一次性grails脚本
更新:从Grails 1.3.6开始,可以从Gant脚本访问完整域。
从Grails 1.3.6发行说明:
您现在可以使用run-script命令从命令行运行一个或多个Groovy脚本,例如
grails run-script [path-to-script-1] [path-to-script-2] ... [path-to-script-n]
这解决了Gant脚本中无法方便地访问应用程序类的问题,因为脚本启动时它们在类路径中不可用。
大家好,
我是新手使用Grails(在一个真实的项目中),我有一个我需要执行的一次性脚本,它读取一个文件,然后填充我的数据库。
我希望脚本在我的grails应用程序的上下文中运行,所以我使用了create-script命令。我现在明白这使它成为一个'Gant'脚本。这样做的原因是我认为它可以让我轻松访问所有grails域的优点,这样我就可以轻松地做到这样的事情:
Car car = new Car(model: 'bar', brand: 'Ford')
car.save()
在这里,Car是我的一个域类,我从文件中检索到了字符串'bar'和'Ford'。
我的脚本的开头看起来像这样:
import com.foo.Car
grailsHome = Ant.project.properties."environment.GRAILS_HOME"
includeTargets << new File ( "${grailsHome}/scripts/Bootstrap.groovy" )
target(main: "a script for storing cars") {
depends(bootstrap, classpath) // code dealing with the file with cars follows
令人惊讶的是,当我使用命令grails LoadCars
执行脚本时,groovy给了我一个java.lang.NoClassDefFoundError: com.foo.Car
我采取了错误的方法,还是有一些更简单的我做错了?
任何帮助表示赞赏
没有找到相关结果
已邀请:
4 个回复
辰炔诚薯
替秀宝
焙恍挝厂熄
扇献隙