从扫描仪获取输入时,Maven挂起 [英] Maven hangs when taking input from Scanner

查看:55
本文介绍了从扫描仪获取输入时,Maven挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个必须使用mvn test命令运行并从命令行获取用户输入的程序.当我使用mvn test运行程序时,一切正常,直到执行 Scanner.next(),然后CLI挂起,我必须关闭程序.

I am designing a program that has to run with the mvn test command and take a user input from the command line. When I run the program with mvn test everything works until Scanner.next() is executed, then the CLI hangs and I have to close the program.

my test method
    public class AppTest 
{
    @Test
    public void shouldAnswerWithTrue()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Awaiting input");
        String in = sc.next();
        System.out.println("TEST!" + in);
        assertTrue( true );
    }
}

我的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>my-app</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
               <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.vogella.build.maven.intro.Main</mainClass>
                </configuration>
            </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

是否可以通过Scanner类以这种方式处理输入?还是完全以这种方式通过maven命令行界面?

Is it possible to handle the input this way with the Scanner class? Or at all through the maven command line interface in this way?

推荐答案

据我所知,您不能通过Maven将Scanner用作任何代码的一部分.Maven可以帮助您构建,测试和部署-实际在运行时并不能帮助您.此外,通常来讲,单元测试不应在运行时依赖输入.

As far as I know you can't use Scanner as part of any code via Maven. Maven is to help you build, test, deploy - not actually help you at runtime. Furthermore, typically speaking, a unit test shouldn't rely on input at runtime.

解决方案1:从命令行直接使用Java直接运行jUnit测试
java -cp.:/usr/share/java/junit.jar org.junit.runner.JUnitCore AppTest

更多信息此处.

解决方案2:使用系统属性
然后,您可以使用 System.getProperty("myVariable");
您将以 mvn -Dtest = shouldAnswerWithTrue -DargLine =-myVariable = abc"

查看有关的更多信息方法.

这篇关于从扫描仪获取输入时,Maven挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆