Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Spring shell and boot together

I am new to Spring, so after whole day of faling attempts I need to ask ;)

Is it possible to combine Spring boot and Spring shell together?

My use case is to build a jar which contains webapp (Spring-boot embeds jetty or tomcat by default) and at the same time is able to execute some project commands from shell. Quarts is not an option. It would be great if these commands and webapp share the same application context.

There are two classes in my src/main/java (plus some commands and controllers in other directories)

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

}

This is my build script:

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

Unfortunatley the resulting jar is spring-shell project, not webapp.
Executing ./build/script/carmonitor (I expected shell project here) outputs

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

Any suggestions what do I do wrong?

like image 567
mrok Avatar asked Apr 11 '16 13:04

mrok


1 Answers

This project on Github worked for me. I cloned the repo, built the project and installed locally. Then used it as a dependency.

And alternative way is described here, where they add all Spring-Shell core components as well as the beans for the JLineShellComponent and the CommandLine to the Boot ApplicationContext. However, it resulted in stack overflow when I tried it.

like image 182
user2824651 Avatar answered Sep 27 '22 21:09

user2824651