Skip to content

Commit 643c635

Browse files
committed
Fix non-interactive shell auto-configuration
- Add test case for non-interactive shells - Add new sample for non-interactive apps Resolves #1226
1 parent 8c95261 commit 643c635

File tree

9 files changed

+172
-0
lines changed

9 files changed

+172
-0
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ You can find complete tutorials and sample applications in the `spring-shell-sam
7272
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-hello-world[Hello World Spring Shell application]
7373
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-spring-boot[Hello World Spring Shell application with Spring Boot]
7474
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-petclinic[Spring Pet Clinic CLI application]
75+
- https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-non-interactive[Non-interactive Spring Shell application]
7576

7677
== Getting Help
7778
Are you having trouble with Spring Shell? We want to help!

spring-shell-core-autoconfigure/src/main/java/org/springframework/shell/core/autoconfigure/ShellRunnerAutoConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ public ApplicationRunner springShellApplicationRunner(ShellRunner shellRunner) {
4343

4444
@Bean
4545
@ConditionalOnMissingClass("org.springframework.shell.jline.DefaultJLineShellConfiguration")
46+
@ConditionalOnProperty(prefix = "spring.shell.interactive", name = "enabled", havingValue = "true",
47+
matchIfMissing = true)
4648
public ShellRunner systemShellRunner(ConsoleInputProvider consoleInputProvider, CommandParser commandParser,
4749
CommandRegistry commandRegistry) {
4850
return new SystemShellRunner(consoleInputProvider, commandParser, commandRegistry);
4951
}
5052

5153
@Bean
5254
@ConditionalOnClass(name = "org.springframework.shell.jline.JLineInputProvider")
55+
@ConditionalOnProperty(prefix = "spring.shell.interactive", name = "enabled", havingValue = "true",
56+
matchIfMissing = true)
5357
public ShellRunner jlineShellRunner(JLineInputProvider inputProvider, CommandParser commandParser,
5458
CommandRegistry commandRegistry) {
5559
return new JLineShellRunner(inputProvider, commandParser, commandRegistry);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.core.autoconfigure;
17+
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.shell.core.NonInteractiveShellRunner;
25+
import org.springframework.shell.core.ShellRunner;
26+
import org.springframework.shell.core.command.annotation.Command;
27+
28+
public class NonInteractiveShellAutoConfigurationTests {
29+
30+
@Test
31+
void testNonInteractiveShellAutoConfiguration() {
32+
// given
33+
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
34+
.withUserConfiguration(SpringShellApplication.class)
35+
.withConfiguration(AutoConfigurations.of(SpringShellAutoConfiguration.class))
36+
.withPropertyValues("spring.shell.interactive.enabled=false");
37+
38+
// when
39+
contextRunner.run(context -> {
40+
// then
41+
assert (context.getBeansOfType(ShellRunner.class).size() == 1);
42+
ShellRunner shellRunner = context.getBean(ShellRunner.class);
43+
Assertions.assertInstanceOf(NonInteractiveShellRunner.class, shellRunner);
44+
});
45+
46+
}
47+
48+
@SpringBootApplication
49+
static class SpringShellApplication {
50+
51+
@Command
52+
public void hi() {
53+
System.out.println("Hello world!");
54+
}
55+
56+
}
57+
58+
}

spring-shell-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>spring-shell-sample-hello-world</module>
1818
<module>spring-shell-sample-petclinic</module>
1919
<module>spring-shell-sample-spring-boot</module>
20+
<module>spring-shell-sample-non-interactive</module>
2021
</modules>
2122

2223
<build>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### About
2+
3+
This is a sample project demonstrating the use of Spring Shell to create a simple non-interactive command-line application based on Spring Boot.
4+
5+
### Building the Project
6+
7+
To build the project, navigate to the project's root directory and run the following command:
8+
9+
```bash
10+
./mvnw clean install
11+
```
12+
13+
### Running the Application
14+
15+
To run the application and invoke the `hi` command in a non-interactive way, use the following command:
16+
17+
```bash
18+
./mvnw -pl org.springframework.shell:spring-shell-sample-non-interactive exec:java -Dexec.mainClass=org.springframework.shell.samples.noninteractive.SpringShellApplication -Dexec.args=hi
19+
```
20+
21+
You should see the result of the `hi` command printed in the console output.
22+
The application will then exit automatically after executing the command.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.springframework.shell</groupId>
7+
<artifactId>spring-shell-samples</artifactId>
8+
<version>4.0.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>spring-shell-sample-non-interactive</artifactId>
12+
<name>Spring Shell Samples - Non interactive Application</name>
13+
<packaging>jar</packaging>
14+
<description>Hello World Spring Boot based Shell application</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.shell</groupId>
19+
<artifactId>spring-shell-starter-jansi</artifactId>
20+
<version>${project.parent.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>ch.qos.logback</groupId>
24+
<artifactId>logback-classic</artifactId>
25+
<version>${logback.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.hibernate.validator</groupId>
29+
<artifactId>hibernate-validator</artifactId>
30+
<version>${hibernate-validator.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.glassfish</groupId>
34+
<artifactId>jakarta.el</artifactId>
35+
<version>${jakarta.el.version}</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
<version>${spring-boot.version}</version>
45+
<configuration>
46+
<mainClass>org.springframework.shell.samples.noninteractive.SpringShellApplication</mainClass>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springframework.shell.samples.noninteractive;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.shell.core.command.annotation.Command;
6+
7+
@SpringBootApplication
8+
public class SpringShellApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(SpringShellApplication.class, args);
12+
}
13+
14+
@Command
15+
public void hi() {
16+
System.out.println("Hello world!");
17+
}
18+
19+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.application.name=spring-shell-non-interactive
2+
spring.shell.interactive.enabled=false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<configuration>
2+
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
5+
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="info">
10+
<appender-ref ref="CONSOLE"/>
11+
</root>
12+
13+
</configuration>

0 commit comments

Comments
 (0)