如何更改 Play、SBT 及其插件使用的 Scala 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15674406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:06:31  来源:igfitidea点击:

How can I change version of Scala that is used by Play, SBT and its plugins?

scalaplayframeworkintellij-ideasbt

提问by expert

Is it necessary to change version of Scala that is used by SBT and its plugins ?

是否有必要更改 SBT 及其插件使用的 Scala 版本?

I'm using Play Framework 2.1.1-RC2. I want to create new project that will use Scala 2.10.1. To achieve that I did following

我正在使用 Play Framework 2.1.1-RC2。我想创建将使用 Scala 2.10.1 的新项目。为了实现这一点,我做了以下

  • added dependency "org.scala-lang" % "scala-compiler" % "2.10.1"in Build.scala
  • initialized variable scalaVersionto 2.10.1 in Build.scala
  • 添加依赖"org.scala-lang" % "scala-compiler" % "2.10.1"Build.scala
  • 初始化变量scalaVersion为 2.10.1 inBuild.scala

Then I ran play aboutcommand and got following output

然后我运行play about命令并得到以下输出

[test2] $ about
Getting Scala 2.10.1 ...
downloading http://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.10.1/scala-compiler-2.10.1.jar ...
        [SUCCESSFUL ] org.scala-lang#scala-compiler;2.10.1!scala-compiler.jar (7401ms)
downloading http://repo1.maven.org/maven2/org/scala-lang/scala-library/2.10.1/scala-library-2.10.1.jar ...
        [SUCCESSFUL ] org.scala-lang#scala-library;2.10.1!scala-library.jar (4309ms)
downloading http://repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.10.1/scala-reflect-2.10.1.jar ...
        [SUCCESSFUL ] org.scala-lang#scala-reflect;2.10.1!scala-reflect.jar (2144ms)
downloading http://repo1.maven.org/maven2/org/scala-lang/jline/2.10.1/jline-2.10.1.jar ...
        [SUCCESSFUL ] org.scala-lang#jline;2.10.1!jline.jar (201ms)
:: retrieving :: org.scala-sbt#boot-scala
        confs: [default]
        5 artifacts copied, 0 already retrieved (24386kB/58ms)
[info] This is sbt 0.12.2
[info] The current project is {file:/C:/work/test_projects/test2/}test2
[info] The current project is built against Scala 2.10.1
[info] Available Plugins: play.Project, sbt.PlayProject, com.typesafe.sbteclipse.plugin.EclipsePlugin, com.typesafe.sbtidea.SbtIdeaPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2

Last line is what concerns me. It says Scala 2.9.2 is used. How can I change that ? Will it affect what version will be used to compile mycode ?

最后一行是我所关心的。它说使用了 Scala 2.9.2。我该如何改变?它会影响用于编译我的代码的版本吗?

Update:Looks like this way of changing version of compiler didn't work properly. After I run my app I've got following warnings in play console:

更新:看起来这种更改编译器版本的方式无法正常工作。运行我的应用程序后,我在播放控制台中收到以下警告:

[warn] Potentially incompatible versions of dependencies of {file:/C:/work/test_projects/test2/}test2:
[warn]    org.scala-lang: 2.10.1, 2.10.0
[info] Compiling 5 Scala sources and 1 Java source to C:\work\test_projects\test2\target\scala-2.10\classes...
[info] 'compiler-interface' not yet compiled for Scala 2.10.1. Compiling...
[info]   Compilation completed in 8.608 s
[warn] Potentially incompatible versions of dependencies of {file:/C:/work/test_projects/test2/}test2:
[warn]    org.scala-lang: 2.10.1, 2.10.0
[info] play - Application started (Dev)
[warn] Potentially incompatible versions of dependencies of {file:/C:/work/test_projects/test2/}test2:
[warn]    org.scala-lang: 2.10.1, 2.10.0

采纳答案by Daniel C. Sobral

SBT is a Scala application, and, like all Scala applications, it needs to be abide by Scala's binary compatibility rules. Specifically, versions 2.8, 2.9 and 2.10 are not compatible with each other.

SBT 是一个 Scala 应用程序,和所有 Scala 应用程序一样,它需要遵守 Scala 的二进制兼容性规则。具体来说,版本 2.8、2.9 和 2.10 彼此不兼容。

That means that, because the SBT version used by Play was compiled with Scala 2.9.2, then all plugins and all project definitionsmust be compiled with another 2.9.x version. This is because your Build.scalais not something that is simply executed by SBT, it becomes partof SBT execution.

这意味着,因为 Play 使用的 SBT 版本是用 Scala 2.9.2 编译的,那么所有插件和所有项目定义都必须用另一个 2.9.x 版本编译。这是因为你Build.scala不是由 SBT 简单执行的东西,它成为SBT 执行的一部分

However, that has relation whatsoever with what version of Scala your application should be compiled with. SBT can use any Scala version, and can even produce multiple Scala versions at the same time -- which is how Scala libraries manage to provide binaries for the multiple Scala versions.

但是,这与您的应用程序应该使用什么版本的 Scala 编译有关。SBT 可以使用任何 Scala 版本,甚至可以同时生成多个 Scala 版本——这就是 Scala 库设法为多个 Scala 版本提供二进制文件的方式。

回答by expert

The ultimate goal of my experiments was to change version of Scala compiler used to build my project.

我实验的最终目标是更改用于构建我的项目的 Scala 编译器的版本。

After multiple attempts I concluded that achieving it nicely not possible. I think it's something creators of Play Framework should address.

经过多次尝试,我得出结论,实现它是不可能的。我认为这是 Play Framework 的创建者应该解决的问题。

Setting scalaVersionto 2.10.1-localin Build.scala didn't help much. SBT seem to be going to Maven/Ivy and downloading pieces of 2.10.0 that I don't want. After compiling the app I get bunch of warnings about incompatible dependencies.

在 Build.scala 中设置scalaVersion2.10.1-local没有太大帮助。SBT 似乎要去 Maven/Ivy 并下载我不想要的 2.10.0 部分。编译应用程序后,我收到一堆关于不兼容依赖项的警告。

I think it happens because {PLAY_HOME}\framework\project\Build.scalahas bunch of variable set to 2.10.0.

我认为这是因为{PLAY_HOME}\framework\project\Build.scala有一堆变量设置为 2.10.0。

"Elegant" solution was to tell SBT I'm going to use local copy of Scala by setting following variables in Build.scala

“优雅”的解决方案是告诉 SBT 我将通过在 Build.scala 中设置以下变量来使用 Scala 的本地副本

scalaVersion := "2.10.1-local",
autoScalaLibrary := false,
scalaHome := Some(file("/Program Files (x86)/scala/"))

But still if I create IntelliJ IDEA configuration with play ideacommand resulting project needs some clean up, in particular:

但是如果我使用play idea命令创建 IntelliJ IDEA 配置,结果项目仍然需要一些清理,特别是:

  • Scala facet references missing Scala 2.10.0library.
  • Module test3-build has dependency on missing Scala 2.9.2library
  • Scala 方面引用缺少的Scala 2.10.0库。
  • 模块 test3-build 依赖于缺失的Scala 2.9.2

I hope my experiments other people who are also learning Play and Scala.

我希望我的实验其他也在学习 Play 和 Scala 的人。

Details are here: http://www.scala-sbt.org/release/docs/Howto/scala.html

详细信息在这里:http: //www.scala-sbt.org/release/docs/Howto/scala.html