用于填充数据库的一次性 grails 脚本

发布于 2024-10-05 12:45:23 字数 1220 浏览 8 评论 0原文

更新:从 Grails 1.3.6 开始,人们可以通过 Gant 脚本访问整个域。 从 Grails 1.3.6 发行说明 中:

您现在可以从以下位置运行一个或多个 Groovy 脚本:使用 run-script 命令的命令行,例如

grails run-script [path-to-script-1] [path-to-script-2]...[path-to-script-n]

这可以解决以下问题您无法方便地访问应用程序类的 Gant 脚本,因为脚本启动时它们在类路径中不可用。


大家好,

我是 Grails 的新手(在实际项目中),我需要执行一个一次性脚本来读取文件,然后填充我的数据库。

我希望脚本在我的 grails 应用程序的上下文中运行,所以我使用了 create-script 命令。我现在明白这使它成为“甘特”脚本。这样做的原因是我认为它可以让我轻松访问所有 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

我是否采取了错误的方法,或者我做错了什么更简单的事情?

任何帮助表示赞赏

Update: as of Grails 1.3.6 one has access to the full domain from Gant scripts.
From the Grails 1.3.6 release notes:

You can now run one or more Groovy scripts from the commandline using the run-script command, e.g.

grails run-script [path-to-script-1] [path-to-script-2]...[path-to-script-n]

This works around the issue in Gant scripts where you can't conveniently access application classes since they're not available in the classpath when the scripts start.


Hi all,

I am new to using Grails (in a real project) and I have a one-off script I need to execute that reads a file and then populates my database.

I wanted the script to run in the context of my grails app, so I used the create-script command. I now understand that makes it a 'Gant' script. The reason for doing so was that I thought it would allow me easy access to all the grails domain good-ness, so that i would be able to do something like this easily:

Car car = new Car(model: 'bar', brand: 'Ford')
car.save()

Here, Car is one of my domain classes and the strings 'bar' and 'Ford' I have retrieved from my file.

The start of my script looks like this:

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

Surprisingly, groovy gives me a java.lang.NoClassDefFoundError: com.foo.Car when I execute the script with the command grails LoadCars

Am I taking the wrong approach, or is there something more simple I am doing wrong?

Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乜一 2024-10-12 12:45:23

我知道这些脚本很有用,而且我可能会因为建议而收到讨厌的邮件,但我过去只是将此类内容直接合并到我的应用程序中。

我在配置中设置了一个标志,指示是否应该引导数据,如果是,则引导代码在启动时查找逗号分隔的文件并调用服务方法来加载数据。

i know the scripts are useful, and I will probably get hate mail for even suggesting it, but I have just incorporating this kinda of stuff directly into my application in the past.

I have a flag set in my configuration which indicates if the data should be bootstrapped, if so, the bootstrap code looks for a comma delimited file at startup and calls a service method to load up the data.

∝单色的世界 2024-10-12 12:45:23

我已经更新了 grails run-script Gant 脚本(上面 Jared 提到的)与 grails 1.3.5 一起使用。我一直想做这件事有一段时间了,但这个问题促使我终于开始做这件事)。

只需下载帖子中描述的脚本,将其保存在 grails“scripts”目录中,然后您就可以运行自己的 groovy 脚本来引导数据:

grails run-script script-path/boostrapMyDataIntoApp.groovy

I've updated the grails run-script Gant script (referred to by Jared above) to work with grails 1.3.5. I'd been meaning to do it for a while, but this question nudged me into finally getting around to it).

Just download the script described in the post, save it in your grails "scripts" directory and you can then run your own groovy script to bootstrap data with:

grails run-script script-path/boostrapMyDataIntoApp.groovy
埖埖迣鎅 2024-10-12 12:45:23

我必须这样做,并且您必须创建一个特殊的脚本来允许您从标准 grails 脚本访问 GORM。有关详细信息,请参阅此问题。我不确定 grails 1.3 下脚本的当前状态是什么,但脚本的作者在评论中发布了。

I've had to do this and you have to create a special script to allow you to access GORM from a standard grails script. See this question for more info. I'm not sure what the current status of the script is under grails 1.3 but the author of the script posted in the comments.

第七度阳光i 2024-10-12 12:45:23

Hans,这里有几个选择,假设您不打算完善 GANT 脚本编写 8^)

所以假设您正在做一些集成模式 TDD,对吗?
您研究过 db-stuff 插件吗?实际上,它利用了名为 dbUnit 的开源包(JUnit 项目的扩展),对于 Java 和 Groovy 项目来说,这也是一个出色的选择。

*db-stuff <0.3.0>; -- 数据库模式管理和数据导入/导出。生成通用模式文件并将基础/种子/测试数据导入或导出到数据库中。


传统上,我也会根据环境在 BootStrap 中执行此操作 - 并且我尝试永远不要让这些域假设/约束变得太不同步。与我的架构。

这是我正在谈论的经典:

class BootStrap {

    def init = { servletContext ->
        if (GrailsUtil.environment.equals( GrailsApplication.ENV_DEVELOPMENT )) {
            log.info( "Loading sample data for 2010 models..." );            

            new Car( manufacturer: new Manufacturer( name: "Toyota" ), model: "Prius" )
            new Car( manufacturer: new Manufacturer( name: "GM" ), model: "Volt" )
//...

Hans, there are several choices here, assuming you are not out to polish the GANT scripting chops 8^)

So assume that you are doing some integration-mode TDD, correct?
Have you looked into the db-stuff plugin? Actually that one leverages the open source package (extension of the JUnit project) called dbUnit, which is also an outstanding choice, for both Java and Groovy projects.

*db-stuff <0.3.0> -- db schema managment and data import/export. Generate generic schema files and import or export base/seed/test data into your database.


I have traditionally done this as well in the BootStrap depending on the environment - and I try to never let those domain assumptions / constraints get too far out of synch. with my schema.

Here's the canon I'm talking about :

class BootStrap {

    def init = { servletContext ->
        if (GrailsUtil.environment.equals( GrailsApplication.ENV_DEVELOPMENT )) {
            log.info( "Loading sample data for 2010 models..." );            

            new Car( manufacturer: new Manufacturer( name: "Toyota" ), model: "Prius" )
            new Car( manufacturer: new Manufacturer( name: "GM" ), model: "Volt" )
//...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文