8

我是春天的新手,所以经过一整天的失败尝试后,我需要问一下;)

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

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

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

应用程序.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);
    }
}

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);
    }

}

这是我的构建脚本:

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'
}

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

Error: Could not find or load main class dk.mrok.carmonitor.Cli

任何建议我做错了什么?

4

1 回答 1

1

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

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

于 2017-03-22T01:16:59.950 回答