将Spring shell 和 Boot 组合在一起

xytpbqjk  于 11个月前  发布在  Spring
关注(0)|答案(2)|浏览(113)

我是新来的Spring,所以经过一整天的失败尝试,我需要问;)
是否可以将合并Spring bootSpring 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);
    }
}

字符串
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


有什么建议我做错了什么吗?

7dl7o3gd

7dl7o3gd1#

This project on Github对我来说很有用。我克隆了repo,构建了项目并在本地安装。然后将其用作依赖项。
另一种方法是here,他们将所有Spring-Shell核心组件以及JLineShellComponent和CommandLine的bean添加到 Boot ApplicationContext。然而,当我尝试时,它导致堆栈溢出。

kd3sttzy

kd3sttzy2#

在我的例子中,我在我的springboot项目中使用了错误版本的springshell,版本太旧了,然后我运行application,仍然没有出来一个shell:> out让我输入;当我使用新版本的springshell时,然后运行application,在springboot的web init之后,“shell:>”条目出现。x1c 0d1x

相关问题