简体   繁体   中英

How to run Grails script and get domain class instance

How to get current application environment in Grails scripts to be able to work with classes and objects like grails console does. My script:

import security.User

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

And I get an error:

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

All other manipulations from Creating an instance of a domain class inside a grails script also does not help.

I see many grails docs are stale and inconsistent.

Grails 3.x removed the run-script command so you have to write your own command. Here is an answer from one of the author of the Grails community:

https://stackoverflow.com/a/30806256/2405040

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM