Skip to content

Commit f44e0c1

Browse files
committed
Create runCommand task to run commands in an application
1 parent 7bc691c commit f44e0c1

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package grails.ui.command
2+
3+
import grails.config.Settings
4+
import grails.dev.commands.ApplicationCommand
5+
import grails.dev.commands.ExecutionContext
6+
import grails.ui.support.DevelopmentGrailsApplication
7+
import groovy.transform.CompileStatic
8+
import org.grails.build.parsing.CommandLine
9+
import org.grails.build.parsing.CommandLineParser
10+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
11+
import org.springframework.context.ConfigurableApplicationContext
12+
13+
/**
14+
* Created by Jim on 8/2/2016.
15+
*/
16+
@CompileStatic
17+
class GrailsRuntimeApplicationContextCommandRunner extends DevelopmentGrailsApplication {
18+
19+
String commandName
20+
21+
protected GrailsRuntimeApplicationContextCommandRunner(String commandName, Object... sources) {
22+
super(sources)
23+
this.commandName = commandName
24+
}
25+
26+
@Override
27+
ConfigurableApplicationContext run(String... args) {
28+
try {
29+
def commandClass = classLoader.loadClass(commandName)
30+
if (ApplicationCommand.isAssignableFrom(commandClass)) {
31+
def command = (ApplicationCommand)commandClass.newInstance()
32+
Object skipBootstrap = command.hasProperty("skipBootstrap")?.getProperty(command)
33+
if (skipBootstrap instanceof Boolean && !System.getProperty(Settings.SETTING_SKIP_BOOTSTRAP)) {
34+
System.setProperty(Settings.SETTING_SKIP_BOOTSTRAP, skipBootstrap.toString())
35+
}
36+
37+
ConfigurableApplicationContext ctx = null
38+
try {
39+
ctx = super.run(args)
40+
} catch (Throwable e) {
41+
System.err.println("Context failed to load: $e.message")
42+
System.exit(1)
43+
}
44+
45+
try {
46+
CommandLine commandLine = new CommandLineParser().parse(args)
47+
ctx.autowireCapableBeanFactory.autowireBeanProperties(command, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false)
48+
command.applicationContext = ctx
49+
def result = command.handle(new ExecutionContext(commandLine))
50+
result ? System.exit(0) : System.exit(1)
51+
} catch (Throwable e) {
52+
System.err.println("Command execution error: $e.message")
53+
System.exit(1)
54+
}
55+
56+
finally {
57+
try {
58+
ctx?.close()
59+
} catch (Throwable e) {
60+
// ignore
61+
}
62+
}
63+
} else {
64+
System.err.println("Command not found")
65+
System.exit(1)
66+
}
67+
} catch (ClassNotFoundException e) {
68+
System.err.println("Command not found")
69+
System.exit(1)
70+
}
71+
return null
72+
}
73+
74+
/**
75+
* Main method to run an existing Application class
76+
*
77+
* @param args The first argument is the Command name, the last argument is the Application class name
78+
*/
79+
public static void main(String[] args) {
80+
if(args.size() > 1) {
81+
Class applicationClass
82+
try {
83+
applicationClass = Thread.currentThread().contextClassLoader.loadClass(args.last())
84+
} catch (Throwable e) {
85+
System.err.println("Application class not found")
86+
System.exit(1)
87+
}
88+
89+
def runner = new GrailsRuntimeApplicationContextCommandRunner(args[0], applicationClass)
90+
runner.run(args.init() as String[])
91+
}
92+
else {
93+
System.err.println("Missing application class name and script name arguments")
94+
System.exit(1)
95+
}
96+
}
97+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.grails.gradle.plugin.commands
2+
3+
import org.gradle.api.tasks.JavaExec
4+
5+
/**
6+
* Created by Jim on 8/2/2016.
7+
*/
8+
class RuntimeApplicationContextCommandTask extends JavaExec {
9+
10+
RuntimeApplicationContextCommandTask() {
11+
setMain("grails.ui.command.GrailsRuntimeApplicationContextCommandRunner")
12+
dependsOn("classes", "findMainClass")
13+
systemProperties(System.properties.findAll { it.key.toString().startsWith('grails.') } as Map<String, Object>)
14+
}
15+
}

grails-gradle-plugin/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import org.grails.build.parsing.CommandLineParser
5252
import org.grails.gradle.plugin.agent.AgentTasksEnhancer
5353
import org.grails.gradle.plugin.commands.ApplicationContextCommandTask
5454
import org.grails.gradle.plugin.commands.ApplicationContextScriptTask
55+
import org.grails.gradle.plugin.commands.RuntimeApplicationContextCommandTask
5556
import org.grails.gradle.plugin.model.GrailsClasspathToolingModelBuilder
5657
import org.grails.gradle.plugin.run.FindMainClassTask
5758
import org.grails.gradle.plugin.util.SourceSets
@@ -122,6 +123,8 @@ class GrailsGradlePlugin extends GroovyPlugin {
122123

123124
configureRunScript(project)
124125

126+
configureRunCommand(project)
127+
125128
configurePathingJar(project)
126129
}
127130

@@ -362,6 +365,9 @@ class GrailsGradlePlugin extends GroovyPlugin {
362365
project.tasks.withType(ApplicationContextScriptTask) { ApplicationContextScriptTask task ->
363366
task.args mainClassName
364367
}
368+
project.tasks.withType(RuntimeApplicationContextCommandTask) { RuntimeApplicationContextCommandTask task ->
369+
task.args mainClassName
370+
}
365371
}
366372

367373
consoleTask.dependsOn(tasks.findByName('classes'), findMainClass)
@@ -510,6 +516,16 @@ class GrailsGradlePlugin extends GroovyPlugin {
510516
}
511517
}
512518

519+
protected void configureRunCommand(Project project) {
520+
project.tasks.create("runCommand", RuntimeApplicationContextCommandTask) {
521+
classpath = project.sourceSets.main.runtimeClasspath + project.configurations.console
522+
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.name)
523+
if (project.hasProperty('args')) {
524+
args(CommandLineParser.translateCommandline(project.args))
525+
}
526+
}
527+
}
528+
513529
protected void configurePathingJar(Project project) {
514530
project.afterEvaluate {
515531
ConfigurationContainer configurations = project.configurations

0 commit comments

Comments
 (0)