java - 当 spring-boot 应用程序需要来自用户的 stdin 时,Maven 构建暂停

标签 java maven spring-boot

我有一个为独立应用程序创建的简单 springboot 项目。 Main方法实现了ApplicationRunner接口(interface)的run方法

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        final Scanner scanner = new Scanner(System.in);
        final String inputFilePath = scanner.nextLine();

        System.out.println(inputFilePath);
    }
}

应用程序可以毫无问题地执行。但是,当通过 Maven 构建应用程序时,构建会在中间暂停并要求输入用户输入。这是因为我使用了扫描仪来获取执行的用户输入

没有对pom文件进行任何更改(这是默认的spring-boot pom)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

简单的java maven项目没有这个问题。它构建成功。这只伴随着这个 spring-boot 项目。我在这里做错了什么?我是否需要向 spring-boot-maven-plugin

添加某种参数

我还编写了几个单元测试。

@RunWith(SpringRunner.class)
@SpringBootTest
public class ValidationTest {

    @Autowired
    private ValidationService validationService;

    @Test
    public void test_1() {
        final String number = "8801673191614";
        boolean result = validationService.isValidMSISDN(number);

        Assert.assertTrue(result);
    }
}

最佳答案

使用 -Dmaven.test.skip=true 参数构建您的项目。问题是,当您运行测试时,所有 Spring Context 都会运行。

关于java - 当 spring-boot 应用程序需要来自用户的 stdin 时,Maven 构建暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56161692/

相关文章:

java - TableView/CellValueFactory - 根据另一个属性的值更改显示的属性

java - 只读 spring-roo 或 spring-web-mvc 中的字段

Java Swing,如何使用鼠标调整 JPanel 的大小?

java - Maven Mojo : Taking complete control over logging/Forbid other plugins to log

java - Spring Boot 中未调用 Spring Batch 作业

spring-boot - 使用 Filebeat 将 Elastic ELK stack 8.5 与 Spring Boots 应用程序集成

spring - 为什么首页无法访问/spring boot应用

java - MyBatis 中的多种返回类型?

java - 取出maven项目的一部分作为模块

java - 公共(public)链部署