Search code examples
scalagradleread-eval-print-loop

How to not have Gradle quit Scala's REPL immediately?


These simple lines in build.gradle expose a repl task that would ideally fire up a scala REPL. Fire up and keep alive that is. After the repl loads, it immediately receives a :quit command and exits.

Important parts of 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
}

Launching the 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 quits automatically immediately after launching. How to not have the REPL quit immediately?


Solution

  • You also need to redirect console input to your javaexec java process. Try adding standardInput System.in to your task definition. In my case, I also found it necessary to add args '-userjavacp'.

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

    and finally running gradle with the -q option suppresses the gradle progress prompts giving me a cleaner scala repl.