如何不让Gradle立即退出Scala的REPL?

时间:2016-02-24 22:22:59

标签: scala gradle read-eval-print-loop

build.gradle中的这些简单行揭示了一个repl任务,理想情况下会触发一个scala REPL。点燃并保持活力。在加载repl之后,它会立即收到:退出命令并退出。

build.gradle的重要部分:

dependencies{
    compile "org.scala-lang:scala-library:2.11.7"
    compile "org.scala-lang:scala-compiler:2.11.7"
}

task repl(type:JavaExec) {
  main = "scala.tools.nsc.MainGenericRunner"
  classpath = sourceSets.main.runtimeClasspath
}

启动REPL:

% gradle repl
:compileJava UP-TO-DATE
:compileScala UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:repl
Welcome to Scala version 2.11.7 (OpenJDK Server VM, Java 1.7.0_91).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :quit

BUILD SUCCESSFUL

Total time: 31.177 secs

REPL在启动后立即自动退出。如何不立即退出REPL?

1 个答案:

答案 0 :(得分:3)

您还需要将控制台输入重定向到javaexec java进程。尝试将standardInput System.in添加到任务定义中。就我而言,我还发现有必要添加args '-userjavacp'

task repl(type:JavaExec) {
  main = "scala.tools.nsc.MainGenericRunner"
  classpath = sourceSets.main.runtimeClasspath
  standardInput System.in
  args '-usejavacp'
}

最后使用-q选项运行gradle会抑制gradle进度提示,为我提供更清晰的scala repl。