In a Grails 3 app, how do I create a CLI command that leverages the app's Service and Domain classes?
The following did not work:
grails create-app test-grails3-angular-cmd --profile=angularcd servergrails create-command MyExampleImplement MyExample:
package test.grails3.angular.cmd import grails.dev.commands.* class MyExampleCommand implements GrailsApplicationCommand { def testService boolean handle() { testService.test() return true } }grails create-service TestServiceImplement TestService:
package test.grails3.angular.cmd import grails.transaction.Transactional @Transactional class TestService { def test() { System.out.println("Hello, test service!") } }grails run-command my-example
Command execution error: Cannot invoke method test() on null object
How can I fix this?
I am using grails 3.3.0.M2.
MyExampleCommandis not a bean, I believe, where service can be injected. However,applicationContextis available inGrailsApplicationCommand(extendsApplicationCommandtrait) which can be leveraged directly to get the service bean.