Grails 3 no longer contains the functionality to execute Scripts within an running application context. Grails 2.x contained the 'run-script' command and also allowed to run arbitrary scripts implemented as Gant tasks under $dir/scripts directory of any project.
As a substitute grails 3 offers ApplicationContext Commands. The downside is that they cannot be implemented in the actual project because they have to be in the gradle buildscript classpath as well. This is also not desirable because one don't want to pollute this classpath with application dependencies.
This project provides a workaround for that. It is on one side a grails project and registers exactly one command that then later at runtime can execute other commands that are only present in the application classpath.
This project has to be in the classpath of the buildscript and at least the runtime classpath of the project.
In project sources:
class TestOtherCommand implements ApplicationCommand {
@Override
boolean handle(ExecutionContext executionContext) {
println 'Hello World'
return true
}
}
grails runCommand test-other [optional arguments to test-other]
or using gradle directly (example calling the generate-tracking command implemented in the application):
./gradlew -Dgrails.command.args='generate-tracking otherArgument' runCommand
No matter if called through gradle or grails directly, the arguments will be in the
executionContext.commandLine.remainingArguments
(Note: For calling from gradle there is a workaround, normally this
wouldn't be the case. -PgrailsArgs is not readable for non dynamic tasks, not prefixed with grails-
.).