将Spring shell和boot组合在一起

mro*_*rok 6 java spring spring-boot spring-shell

我是Spring的新手,所以经过一整天的尝试,我需要问;)

是否可以将Spring引导Spring shell结合在一起?

我的用例是构建一个包含webapp的jar(默认情况下Spring-boot嵌入jetty或tomcat),同时能够从shell执行一些项目命令.Quarts不是一个选择.如果这些命令和webapp共享相同的应用程序上下文,那将是很好的.

我的src/main/java中有两个类(加上其他目录中的一些命令和控制器)

Application.java

package dk.mrok.carmonitor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Bootstrap webapp
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Cli.java

package dk.mrok.carmonitor;

import java.io.IOException;
import org.springframework.shell.Bootstrap;

public class Cli {

    /**
     * Main class that delegates to Spring Shell's Bootstrap class in order to simplify debugging inside an IDE
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        Bootstrap.main(args);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的构建脚本:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'application'
apply plugin: 'idea'

jar {
    baseName = 'carmonitor-backend'
    version =  '0.0.1'
}

repositories {
    mavenCentral()
    maven {url 'https://repo.spring.io/libs-snapshot'}
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

mainClassName = "dk.mrok.carmonitor.Cli"

dependencies {
    compile 'org.springframework.shell:spring-shell:1.2.0.BUILD-SNAPSHOT'
    compile "org.springframework.boot:spring-boot-starter-web"
    testCompile "junit:junit"
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}
Run Code Online (Sandbox Code Playgroud)

Unfortunatley生成的jar是spring-shell项目,而不是webapp.
执行./build/script/carmonitor(我在这里期望shell项目)输出

Error: Could not find or load main class dk.mrok.carmonitor.Cli
Run Code Online (Sandbox Code Playgroud)

有什么建议我做错了吗?

小智 1

Github 上的这个项目对我有用。我克隆了存储库,构建了项目并在本地安装。然后将其用作依赖项。

这里描述了替代方法,他们将所有 Spring-Shell 核心组件以及 JLineShellComponent 和 CommandLine 的 bean 添加到 Boot ApplicationContext。然而,当我尝试时,它导致了堆栈溢出。