如何 运行 Grails 脚本和获取域 class 实例

How to run Grails script and get domain class instance

如何在 Grails 脚本中获取当前应用程序环境,以便能够使用 类 和 grails console 之类的对象。 我的脚本:

import security.User

List users = User.list()
users.each { User user ->
    print(user.name)
}

我得到一个错误:

my-script.groovy: 1: unable to resolve class security.User

Creating an instance of a domain class inside a grails script 的所有其他操作也无济于事。

我看到许多 grails 文档陈旧且不一致。

Grails 3.x 删除了 run-script 命令,因此您必须编写自己的命令。以下是 Grails 社区作者之一的回答:

As an alternative, you can write an ApplicationContextCommand see for example, the schema-export command