大致:想要使用Spring Shell,则项目需要是 Spring Boot项目,下面贴出结构和代码
1.POM依赖
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE my SpringShellDemo 1.0-SNAPSHOT SpringShellDemo http://www.example.com 1.8 org.springframework.boot spring-boot-starter-web 2.1.1.RELEASE org.springframework.boot spring-boot-starter-test 2.1.1.RELEASE test org.springframework.boot spring-boot-starter 2.1.1.RELEASE org.springframework.shell spring-shell-starter 2.0.0.RELEASE org.springframework.boot spring-boot-maven-plugin 2.0.0.RELEASE
2.DemoApplication.java Spring Boot启动类
package command;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
3.MyCommand.java 命令类
package command;import org.springframework.shell.standard.ShellComponent;import org.springframework.shell.standard.ShellMethod;@ShellComponentpublic class MyCommandDemo { @ShellMethod("Add two intergers together.") public int add(int a,int b){ return a+b; }}
如果IDE工具是IDEA,执行
mvn clean install -DskipTests:跳过测试,直接安装Installjava -jar target\**.jar(安装的jar包名称):执行jar包
问题:如果启动时出现未找到主程序,则需查看主POM文件是否添加Parent的Spring Boot依赖