How can I define a default command for a Spring Boot Shell application?
Right now I have to enter java -jar app.jar calculate-cognitive-metrics ".\src\main\java\"
I want to change this to java -jar app.jar ".\src\main\java\"
so that it executes this command by default.
Also, when I try to just run java -jar app.jar --help
I get this org.springframework.shell.CommandNotFound: No command found for '--help'
instead of the help output. I’m new to Java and Spring Shell and I’ve already checked the docs, but I don’t really get how to configure it properly to do what I want.
CognitiveAnalysisApplication.java
@SpringBootApplication
public class CognitiveAnalysisApplication implements ApplicationRunner {
public static void main(final String[] args) {
SpringApplication.run(CognitiveAnalysisApplication.class, args);
}
@Override
public void run(final ApplicationArguments args) throws Exception {
}
}
ShellCommands.java (excerpt)
@ShellMethod(key = "calculate-cognitive-metrics")
public void calculateCognitiveMetrics(
final @ShellOption(defaultValue = "src/main/java") String directoryPath,
final @ShellOption(defaultValue = "", value = "--html-report") String htmlReport,
final @ShellOption(defaultValue = "", value = "--csv-report") String csvReport,
final @ShellOption(defaultValue = "", value = "--json-report") String jsonReport
) throws IOException {
1
Use this command in the terminal:
java -jar app.jar calculate-cognitive-metrics --help
Example code:
@SpringBootApplication
@ShellComponent
public class DemoShellApplication {
@ShellMethod(key = "calculate-cognitive-metrics")
public void calculateCognitiveMetrics(
final @ShellOption(defaultValue = "src/main/java") String directoryPath,
final @ShellOption(defaultValue = "", value = "--html-report") String htmlReport,
final @ShellOption(defaultValue = "", value = "--csv-report") String csvReport,
final @ShellOption(defaultValue = "", value = "--json-report") String jsonReport
) throws IOException {
}
public static void main(String[] args) {
SpringApplication.run(DemoShellApplication.class, args);
}
}
Output from my local:
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.3)
2024-09-13T09:01:56.437+05:30 INFO 28814 --- [demo-shell] [ main] com.example.DemoShellApplication : Starting DemoShellApplication v0.0.1-SNAPSHOT using Java 21.0.1 with PID 28814 (/Users/anish/demo-shell-0.0.1-SNAPSHOT.jar started by anish in /Users/anish)
2024-09-13T09:01:56.440+05:30 INFO 28814 --- [demo-shell] [ main] com.example.DemoShellApplication : No active profile set, falling back to 1 default profile: "default"
2024-09-13T09:01:57.545+05:30 INFO 28814 --- [demo-shell] [ main] com.example.DemoShellApplication : Started DemoShellApplication in 1.582 seconds (process running for 2.192)
NAME
calculate-cognitive-metrics -
SYNOPSIS
calculate-cognitive-metrics --directoryPath String --html-report String --csv-report String --json-report String --help
OPTIONS
--directoryPath String
[Optional, default = src/main/java]
--html-report String
[Optional]
--csv-report String
[Optional]
--json-report String
[Optional]
--help or -h
help for calculate-cognitive-metrics
[Optional]