3

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.

Community
  • 1
  • 1
Alex Shwarc
  • 822
  • 10
  • 20
  • How are running your script and where it is located? – Shashank Agrawal Sep 14 '15 at 15:26
  • in default scripts folder under src/main/scripts because I used create-script command. How I run it?? Hmm, already forgot, I got a error when i just launch "grails". By the way, I see there is not "run-script" command anymore.. So how to run scripts and how to use it. This article seems to be outdated http://www.componentix.com/blog/15/execute-groovy-script-within-grails-context-updated-for-grails – Alex Shwarc Sep 14 '15 at 16:45
  • possible duplicate of [run-script alternative in Grails 3](http://stackoverflow.com/questions/30591547/run-script-alternative-in-grails-3) – Shashank Agrawal Sep 14 '15 at 17:02

1 Answers1

2

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

Community
  • 1
  • 1
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • Maybe someone can give a concrete example of that ? https://github.com/grails/grails-data-mapping/blob/master/grails-plugins/hibernate/src/main/groovy/grails/plugin/hibernate/commands/SchemaExportCommand.groovy is not clear enough to implement. I really suffering of absence Yii.Command alternative to Grails. – Alex Shwarc Sep 16 '15 at 07:39