8

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?

mrok
  • 2,570
  • 3
  • 25
  • 42
  • 1
    Just make it a web application, add the `spring-boot-starter-remote-shell` and write your commands. That should work. You shouldn't create 2 separate applications (that is what you are doing right now). – M. Deinum Apr 11 '16 at 14:04
  • Using spring-boot-starter-remote-shell helps a lot. thanks – mrok Apr 17 '16 at 21:29
  • @mrok how did you solve this ? – vishal Sep 28 '16 at 10:54
  • @vishal please take a look at example https://github.com/mrok/spring-web-shell-example – mrok Oct 02 '16 at 15:29

1 Answers1

1

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.

user2824651
  • 15
  • 1
  • 5