What is the difference of running `scala` and `sbt

2020-05-23 02:15发布

问题:

What is the difference of running the scala shell in these different ways?

回答1:

SBT is tied to a specific project defined by a build.sbt file in a way that $ sbt console will load up the same REPL environment as $ scala but with the addition of all of the project code and dependencies defined in the build available for import. Also, it will use the version of Scala defined by build.sbt.

For example:

$ scala
scala> import scalaz._
<console>:7: error: not found: value scalaz
       import scalaz._

but with this build.sbt:

scalaVersion := "2.11.4"

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0"

the import succeeds:

$ sbt console
...
scala> import scalaz._
import scalaz._

The command loads Scala 2.11.4 instead of the system wide Scala (or any version of it on PATH).

Furthermore, invoking sbt console after adding new items in the build’s libraryDependencies will fetch them.



回答2:

If you call scala, you will get whatever scala version is installed on the path of your operating system.

If you call sbt console, you get the scala version configured in the sbt build (build.sbt) with all libraries that are used in the build already on the classpath.

See this answer for details.



标签: scala sbt