Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala REPL in Gradle

At the moment Gradle's scala integration does not offer REPL functionality. How to ergonomically run a Scala REPL from Gradle with the appropriate classpath?

like image 920
Dominykas Mostauskis Avatar asked Feb 25 '16 15:02

Dominykas Mostauskis


People also ask

Does Gradle support Scala?

Gradle supports version 1.6. 0 of Zinc and above. The Zinc compiler itself needs a compatible version of scala-library that may be different from the version required by your application. Gradle takes care of specifying a compatible version of scala-library for you.

What is Gradle Scala?

Gradle is a build tool that can be used easily with a large number of programming languages including Scala. With it you can easily define your builds for Groovy or Kotlin, which enables for a high degree of customization.

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, binary plugins and script plugins.


Video Answer


1 Answers

Minimal build.gradle:

apply plugin: 'scala'

repositories{
  mavenCentral()
}

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
  standardInput System.in
  args '-usejavacp'
}

Credit to this answer for explaining how to direct stdin with standardInput and have REPL use the right classpath with args.

Notice the scala-compiler library is a dependency. That's where scala.tools.nsc.MainGenericRunner is found.

From the console a number of options are needed to run the REPL:

  • --no-daemon, if you are using a Gradle daemon. At the moment, the REPL does not respond to keystrokes if run from the daemon.

  • --console plain. A popular, but inferior alternative is --quiet. If run without one of these options, REPL's prompt is contaminated by Gradle's progress report. --console plain has the advantage that it also adjusts readline's behaviour so that rlwrap is unnecessary.

Full command to run the REPL is gradle repl --console plain --no-daemon, so creating an alias in your shell makes sense.

like image 50
Dominykas Mostauskis Avatar answered Oct 19 '22 19:10

Dominykas Mostauskis