sbt configuring -- or using an already installed -- newer version of Scala - scala

When I install the latest version of sbt (v0.13.9) and then run the following to download scala and associated libraries:
jdoe$ sbt test
jdoe$
jdoe$ sbt console
[info] Set current project to nmvega (in build file:/home/jdoe/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_31).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
I get Scala version 2.10.5, as seen above.
How can I get sbt to build with (or use a separately installed) scala version, say v2.12.0-M3 ? I can't find instructions for this.
Thank you in advance.

As you can see in documentation SBT provides many ways to Configure and use Scala
Set the Scala version used for building the project
The scalaVersion configures the version of Scala used for compilation. By default, sbt also adds a dependency on the Scala library with this version. See the next section for how to disable this automatic dependency. If the Scala version is not specified, the version sbt was built against is used. It is recommended to explicitly specify the version of Scala.
For example, to set the Scala version to "2.9.2",
scalaVersion := "2.9.2"
Disable the automatic dependency on the Scala library
sbt adds a dependency on the Scala standard library by default. To disable this behavior, set the autoScalaLibrary setting to false.
autoScalaLibrary := false
Temporarily switch to a different Scala version
To set the Scala version in all scopes to a specific value, use the ++ command. For example, to temporarily use Scala 2.8.2, run:
> ++ 2.8.2
Use a local Scala installation for building a project
Defining the scalaHome setting with the path to the Scala home directory will use that Scala installation. sbt still requires scalaVersion to be set when a local Scala version is used. For example,
scalaVersion := "2.10.0-local"
scalaHome := Some(file("/path/to/scala/home/"))

Can you post your build.sbt please.
You should be able to declare the Scala by adding the following to build.sbt file
scalaVersion := "2.11.7"

Related

Which version of SBT ? scala2.13.3

According to the SBT website, the latest version of SBT is 1.3.13. Is this available in 2.13.3, the latest version of Scala?
Yes.
You can download the latest version of sbt (1.3.13 for now)
https://www.scala-sbt.org/download.html
and use it for your project if you write in the file project/build.properties
sbt.version = 1.3.13
Also you can write in the file build.sbt
scalaVersion := "2.13.3"
Currently it's the latest version of Scala
https://www.scala-lang.org/download/all.html
So you will be using the latest versions both of Scala and sbt.
Some sideways remarks. Sbt is written in Scala itself. For sbt 1.3.13 it's Scala 2.12.10. Most probably this version of Scala is irrelevant to you. It's relevant only to those who develop sbt or write plugins to sbt etc. Also the file build.sbt is a script in Scala using version of Scala specified in project/build.sbt i.e. in the build file of meta-project (sbt is recursive). It should be Scala 2.12.x because of the binary compatibility with the version of Scala sbt is written in, i.e. from 2.12.0 to 2.12.12. So you can write val x: 1 = 1 (singleton types, a Scala-2.13 feature) in your project Scala code but can't in build.sbt. In this sense the answer is No.

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?
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.

SBT, Scala version mismatch

i have installed Scala 2.11.8 version, and i installed SBT 0.13.12 version as well.
when i create one directory and inside that directory typed sbt and sbt prompt opened. Inside SBT prompt, when i checked libraryDependencies i found like this:
sbt:sparkp> libraryDependencies
[info] * org.scala-lang:scala-library:2.12.4
But in machine, i have installed scala 2.11.8 version, when i checked scala -version it shows
hadoop#localhost:~$ scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
Did i made any mistake while installing sbt? or i need to change any configuration properties after installation?
please help out.
Note: There are some questions regarding this error in stackoverflow, but i didn't get exact answer that's why i am asking new question.
In my opinion the scala version you are getting is the version with which sbt is built with.
The solution to your is to find global.sbt file which is usually in ~/.sbt/<version>/global.sbt
and add following line
scalaVersion := "2.11.8"
Your problem should be solved.
The Scala version of a project in SBT is completely unrelated to one you have installed and available from command line. This is a very good thing for several reasons:
Everyone who builds a project doesn't get different result depending on Scala version they have installed.
You don't need to install multiple Scala versions and switch between them all the time to work on multiple projects.
Many projects should be built with multiple Scala versions, see http://www.scala-sbt.org/0.13/docs/Cross-Build.html.
If you are creating a new project, you should specify scalaVersion := "2.11.8" (or any other you want) in build.sbt in this project.

sbt is using wrong scala version rather than using the configuration in build.sbt

I have put these in the build.sbt file under current project's root directory
scalaHome := Some(file("/Users/ddam/scala-2.10.2"))
scalaVersion := "2.10.2"
And I ran sbt using
$ sbt --version
sbt launcher version 0.12.4
But still, I am seeing both the wrong version for sbt and scala when a particular dependency cannot be resolved
[warn] Note: Some unresolved dependencies have extra attributes. Check that these dependencies exist with the requested attributes.
[warn] com.typesafe.sbteclipse:sbteclipse-plugin:2.4.0 (sbtVersion=0.12, scalaVersion=2.9.2)
Please help me.
It looks like sbteclipse requires SBT 0.13.0, and you are using version 0.12.4.
You can specify the SBT version by following the directions on this page.
Some other notes: You probably want to use Scala 2.10.3, not 2.10.2.
Also, it's strange to specify scalaHome; usually SBT will automatically fetch the needed Scala jars for you.
So to bootstrap a Scala environment, all you need to have installed are SBT and a JDK.
EDIT: (addressing comment below):
When you build code with SBT, you may actually use two different versions of Scala.
There is the version for SBT (what version of Scala the build system runs on), and the version for the code in your project (what version of Scala your code will run on).
The Scala version for SBT is determined by the SBT version you use.
If you use 0.12.4, SBT will run on Scala 2.9.3.
If you use 0.13.0, SBT will run on Scala 2.10.3.
You control the SBT version by following these instructions.
To control the version of Scala your project will run on, you can set scalaVersion in <projectRoot>/build.sbt.
So, you're getting that error because you're using SBT 0.12.4, which uses Scala 2.9.3.
SBT tries to find the sbteclipse plugin for 2.9.3, but it doesn't exist because it requires SBT 0.13.0 (=> Scala 2.10.3).
It seems you've correctly specified the use of Scala 2.10.2 for compiling your project.
sbt 0.12.4 always uses Scala 2.9.x to compile the build files (i.e. build.sbt and the stuff under project/). This means that plugins for sbt 0.12.x must also be compiled against 2.9.x, which explains what you're seeing.
sbteclipse 2.4.0 requires sbt 0.13. Try sbteclipse 2.2.0 if you want to stay on sbt 0.12.4.
emchristiansen's answer has good additional points.

SBT 0.10 - remove dependency on Scala 2.8.1

My project uses Scala 2.9 so I've added
scalaVersion := "2.9.0-1"
to my build.sbt file, and run gen-idea. However IDEA still shows Scala 2.8.1 (in addition to 2.9) in External Libraries. Why, and how to remove it?
Your application will be compiled with whatever Scala version or versions you choose. However, SBT is also a Scala application, and it was compiled with Scala 2.8.1, so it needs Scala 2.8.1's libraries to work. This will not have any impact on your application.