How to change Scala version for sbt project? - scala

How can I change Scala version in a sbt project?
I would like SBT to check whether the system's Scala version is correct and if it is not the case then download it.

xsbt (0.10+, including the latest 0.13.7)
Change scalaVersion in build.sbt to whatever Scala version your project should be using - see .sbt build definition.
scalaVersion := "2.10.1"
sbt:
As mentioned in RunningSBT, you can:
You can temporarily switch to another version of Scala using ++<version>.
This version does not have to be listed in your build.scala.versions property, but it does have to be in a repository or be a local Scala version you have defined.
But the CrossBuild page is more suited for what you want, as it shows in action how to change the build.scala.versions property.
So you should be able to
set build.scala.versions 2.7.7
reload
set build.scala.versions 2.8.0.RC2
reload
and each time trigger a compilation with a different Scala version.

Related

Is there a way to define the required sbt version in build.sbt?

In the build.sbt definition of a Scala project, is it possible to define the minimum version of sbt that is required to build the project?
project/build.properties allows you to force sbt to use particular version. If current version of installed sbt is different - sbt itself will download (if needed) the version you've specified there:
sbt.version=0.12.0
See, Hello for instance. So actually you may have installed several versions of sbt in your system - it's just jars, that placed in .sbt/boot/scala-{scala.version required by this sbt}/org.scala-sbt/sbt/{sbt.version} folder. Sbt executable actually looks for version specified in project/build.properties or (if it's not specified) highest version installed in the system.
P.S. From sbt-launcher perspective, sbt is only one dependency with small non-intersecting others like ansi.jar, so specifying the range of versions like [0.13.1, 0.13.8] have no much sense as it would be efective only for conflicting transitive deps. Otherwise it's enough to specify higher possible version (which would compile) - you may even choose some version that you already have (and update project's sbt that way).

Multi Scala version on one linux machine

Im working on several Scala projects and liberties
Some of them work on scala 2.10.x version and some of them work on the 2.11.X version .
My default $scala_home version is 2.11 so if a build something by default it will be built in the 2.11 version? (thats true??)
My main issue is with Apache Spark and Kafka that are working fine with the 2.10.x version .
How are handling multi version dependency on one machine.
thanks,
miki
I would not install a global Scala on the system, neither do I have a $SCALA_HOME. I think most people would recommend against it.
If you use a build tool like sbt, that will take care of applying the correct Scala version as required by your build definition. In sbt, you have a file build.sbt that contains an assignment scalaVersion := "2.10.4" or scalaVersion := "2.11.5" depending on which version you want to use. It keeps all the different Scala versions neatly separated in a cache directory and won't confuse them.

Do you need to install Scala separately if you use sbt?

Reason I ask, is because it's possible to specify a Scala version in the build.sbt file (using scalaVersion setting), and once you do that, sbt will automatically download that Scala version to use with the project.
I also seem to remember that despite having Scala 2.11.1 on my system, sbt would compile and run with Scala 2.10 if no version was specified.
So the question is, do I need to install Scala separately if I got sbt installed?
No you don't need it. sbt will download Scala for you.
If you install sbt-extras (basically just a script) you don't even need to download sbt: it will automatically download the sbt launcher you need. Very handy since you just need to specify sbt.version in your build.properties and you're good to go.
Edit: removed my comment about not being able to do sbt console in an empty directory, since both sbt and sbt-extras support it now.

how to configure SBT to pick the desired scala version (2.9)

While creating a project with sbt command it always prompts for the 2.7.X version of scala however I have 2.9.0 and sbt 0.7.7 installed is there a way to configure sbt to pick 2.9 by default.
If you use sbt version 0.10 instead it has changed a bit, in the build.sbt file you specify scalaVersion := "2.9.0-1" (default seem to be 2.8.1)
See Migrating from SBT 0.7.x to 0.10.x or Quick Configuration Examples. The Full configuration example shows Scala style configuration.
Threre is a ~/.sbt/plugins/ library where you store global plugins. But I do not yet know if you can define a global build properties.
In your project directory there should be a file called build.properties. There you can configure SBT to use whatever version you want. When you change the file either exit SBT or use the command reload.

How do i change the Scala version that sbt works with?

Firing up the SBT console it reads :
[info] Building project AYLIEN 1.0 against Scala 2.8.1
[info] using MyProject with sbt 0.7.4 and Scala 2.7.7
How can I make it use MyProject with sbt 0.7.4 and Scala 2.8.1 ? Please pay attenetion that I'm not asking about the Scala version that is used to build my project (it is the 2.8.1 as you can see), but I rather want to make sbt use MyProject with Scala 2.8.1. Apparently sbt uses it's own scala version to work with project definition (MyProject here) which is different than one it uses to actually build the project! or perhaps I'm missing something ... ?
I can see your concern about SBT still using 2.7.7 internally, but it doesn't really matter since SBT downloads that version on its own. You do not have to install 2.7.7 or anything, just forget about it and pretend your environment is pure Scala 2.8.
The configuration file that holds the SBT version setting is: project/build.properties. The content looks like this:
project.organization=com.ab.web
project.name=cool_proj
sbt.version=0.7.4
project.version=1.0
build.scala.versions=2.8.0
project.initialize=false
When you want to move up to the next SBT version, just change 0.7.4 to that version and SBT will update itself. Eventually SBT will use some other Scala version internally, but this will not matter to the user.
SBT 0.7.* won't work with Scala 2.8.* for your project definition.
Mark Harrah is currently working on the next version of SBT which will work with 2.8.*. This means that you can't use any Scala features or functionality that was added after Scala 2.7.7 in your project definition or plugins. Your project itself is free to use 2.8.*.