我有一个抽象的 groovy 类,其中包含一些我想从其他 groovy 命令类扩展的实用方法(用于 Spring Boot 的远程 shell)。但是,当我尝试运行 groovy 命令类时,我得到一个CommandException
.
我的 groovy 抽象类如下所示。
package commands
import com.xyz.MyService
import org.crsh.command.InvocationContext
import org.springframework.beans.factory.BeanFactory
abstract class abstractcmd {
private static final String SPRING_FACTORY = "spring.beanfactory"
protected MyService getMyService(InvocationContext context) {
return getBeanFactory(context).getBean(MyService.class);
}
private BeanFactory getBeanFactory(InvocationContext context) {
return context.attributes[SPRING_FACTORY];
}
}
我的 groovy 命令类如下所示。
package commands
import org.crsh.cli.Command
import org.crsh.cli.Usage
import org.crsh.command.InvocationContext
@Usage("do something commands")
class foo extends abstractcmd {
@Command
@Usage("bar")
def String bar(InvocationContext context) {
try {
getMyService(context).bar()
return "did bar"
} catch (Exception e) {
return String.format("could not do bar: %s", e.toString())
}
}
}
当我 SSH 进入 shell 并执行时,foo bar
我得到以下异常。
org.crsh.shell.impl.command.spi.CommandException:无法创建命令 foo 实例 在 org.crsh.lang.impl.groovy.GroovyCompiler$1.getCommand(GroovyCompiler.java:192) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.LanguageCommandResolver.resolveCommand(LanguageCommandResolver.java:101) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.command.CRaSH.getCommand(CRaSH.java:100) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.command.CRaSHSession.getCommand(CRaSHSession.java:96) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.impl.script.PipeLineFactory.create(PipeLineFactory.java:89) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.impl.script.ScriptRepl.eval(ScriptRepl.java:88) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.command.CRaSHSession.createProcess(CRaSHSession.java:163) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.async.AsyncProcess.execute(AsyncProcess.java:172) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.Console.iterate(Console.java:219) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.Console.on(Console.java:158) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.Console.on(Console.java:135) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.jline.JLineProcessor.run(JLineProcessor.java:204) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.ssh.term.CRaSHCommand.run(CRaSHCommand.java:99) ~[crash.connectors.ssh-1.3.2.jar:?] 在 java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]
如果我从和类中删除该package commands
行,那么 IDE (IntelliJ) 和 shell 都会抱怨找不到。abstractcmd
foo
abstractcmd
2017-03-19 19:34:06 错误 org.crsh.shell.impl.command.CRaSHProcess.execute:84 - 评估请求“foo”时出错,无法编译命令脚本 foo org.crsh.shell.impl.command.spi.CommandException:无法编译命令脚本 foo 在 org.crsh.lang.impl.groovy.GroovyClassFactory.parse(GroovyClassFactory.java:65) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.impl.groovy.GroovyCompiler$1.getCommand(GroovyCompiler.java:172) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.LanguageCommandResolver.resolveCommand(LanguageCommandResolver.java:101) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.command.CRaSH.getCommand(CRaSH.java:100) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.command.CRaSHSession.getCommand(CRaSHSession.java:96) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.impl.script.PipeLineFactory.create(PipeLineFactory.java:89) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.lang.impl.script.ScriptRepl.eval(ScriptRepl.java:88) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.command.CRaSHSession.createProcess(CRaSHSession.java:163) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.shell.impl.async.AsyncProcess.execute(AsyncProcess.java:172) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.Console.iterate(Console.java:219) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.Console.on(Console.java:158) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.Console.on(Console.java:135) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.console.jline.JLineProcessor.run(JLineProcessor.java:204) ~[crash.shell-1.3.2.jar:?] 在 org.crsh.ssh.term.CRaSHCommand.run(CRaSHCommand.java:99) ~[crash.connectors.ssh-1.3.2.jar:?] 在 java.lang.Thread.run(Thread.java:745) [?:1.8.0_111] 引起:org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败: foo: 9: 无法解析类 abstractcmd @ 第 9 行,第 1 列。 @Usage("条") ^ 1 个错误 在 org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310) ~[groovy-2.4.7.jar:2.4.7] 在 org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946) ~[groovy-2.4.7.jar:2.4.7] 在 org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593) ~[groovy-2.4.7.jar:2.4.7] 在 org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542) ~[groovy-2.4.7.jar:2.4.7] 在 groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) ~[groovy-2.4.7.jar:2.4.7] 在 groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) ~[groovy-2.4.7.jar:2.4.7] 在 org.crsh.lang.impl.groovy.GroovyClassFactory.parse(GroovyClassFactory.java:59) ~[crash.shell-1.3.2.jar:?] ... 14 更多
abstractcmd
如果我不扩展(从而在命令类中实现“util”方法)并删除该package
行,我能够使自定义命令正常工作的唯一方法是。
我正在使用 Spring Boot v1.5.1.RELEASE。
关于我做错了什么的任何想法?