Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the relationship of the versions of scala when I use sbt to build a scala project?

I'm building a scala project(writen in scala 2.11) with SBT 1.x.There are a few "versions of scala" which made me puzzle.

SBT 1.x    => scala 2.12   
SBT plugin => scala 2.x   
My project => scala 2.11   

Please help me to figure out what's the difference or relationship between them.And how SBT tells them apart when compiling or running the project?

like image 484
dukyz Avatar asked Feb 27 '18 02:02

dukyz


People also ask

What is the difference between sbt and Scala?

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.

Does sbt need Scala?

sbt needs to obtain Scala for a project and it can do this automatically or you can configure it explicitly. The Scala version that is configured for a project will compile, run, document, and provide a REPL for the project code.

What is sbt based Scala project?

sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.

Which of the following build tools can be used to build Scala projects?

You can use several different tools to build your Scala projects, including Ant, Maven, Gradle, and more.


1 Answers

The Scala version used by sbt itself and its plugins is completely independent from the Scala version used to compile the code in your project. The sbt version determines the Scala version it uses:

  • sbt 0.13 uses Scala 2.10
  • sbt 1.x uses Scala 2.12

You can set this version in project/build.properties, for example:

sbt.version = 1.1.1

The sbt plugins you want to use have to be compatible with the given version of sbt (and many are cross-compiled with both 0.13 and 1.x).

To set the version of Scala you want to use for the code in you project, use scalaVersion setting in your build.sbt:

scalaVersion := "2.12.4"

Again, it's independent from the version for sbt. You can also cross-compile your code for several Scala versions:

scalaVersion := "2.12.4"
crossScalaVersions := Seq("2.11.12", "2.12.4")

Then if you run compile in sbt, it will use Scala 2.12.4, and if you run +compile, it will first compile it with Scala 2.11.12 and then with 2.12.4. See sbt docs for more about Cross-building.

like image 142
laughedelic Avatar answered Sep 21 '22 19:09

laughedelic