scala 如何强制 SBT 使用 Java 8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25926111/
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
How to force SBT to use Java 8?
提问by Phil
How can I force SBT to compile to Java 8 class files. I added scalacOptions += "-target:jvm-1.8" but it gives the following error message:
如何强制 SBT 编译为 Java 8 类文件。我添加了 scalacOptions += "-target:jvm-1.8" 但它给出了以下错误消息:
[error] 'jvm-1.8' is not a valid choice for '-target'
[error] bad option: '-target:jvm-1.8'
[error] (compile:compile) Compilation failed
I am using SBT version 0.15.5.
我正在使用 SBT 版本 0.15.5。
I know I am using Java 8 to compile as I added this to build.sbt, but I still wonder why the scalacOptions fails and I don't know what output the scalac produces.
我知道我正在使用 Java 8 进行编译,因为我将它添加到 build.sbt,但我仍然想知道为什么 scalacOptions 失败,我不知道 scalac 产生什么输出。
initialize := {
val _ = initialize.value
if (sys.props("java.specification.version") != "1.8")
sys.error("Java 8 is required for this project.")
}
回答by Renato
You need the following on your build.sbt file.
您的 build.sbt 文件需要以下内容。
javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint")
initialize := {
val _ = initialize.value
val javaVersion = sys.props("java.specification.version")
if (javaVersion != "1.8")
sys.error("Java 1.8 is required for this project. Found " + javaVersion + " instead")
}
回答by Kenji Yoshida
Support in scalac for jvm-1.8 was added in 2.11.4.
在 2.11.4 中添加了 scalac 对 jvm-1.8 的支持。
Scala version (2.11.2) does not support -target:jvm-1.8option.
Scala 版本 (2.11.2) 不支持-target:jvm-1.8选项。
$ scala -version
Scala code runner version 2.11.2 -- Copyright 2002-2013, LAMP/EPFL
$ scala -target
Usage: -target:<target>
where <target> choices are jvm-1.5, jvm-1.6, jvm-1.7 (default: jvm-1.6)
bad option: '-target'
Usage: scala <options> [<script|class|object|jar> <arguments>]
or scala -help
All options to scalac (see scalac -help) are also allowed.

