在 sbt 中为 Scala 项目强制执行 Java 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19208942/
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
Enforcing Java version for Scala project in sbt?
提问by Henry Story
My scala application will only run with Java 7 as it depends on libraries that only appeared in that version of the JDK.
我的 scala 应用程序只能使用 Java 7 运行,因为它依赖于仅出现在该版本的 JDK 中的库。
How do I enforce that in sbt, so that the correct error message is shown immediately to the user if she is using the wrong version of Java when starting sbt to run/compile the application?
我如何在 sbt 中强制执行,以便在用户启动 sbt 运行/编译应用程序时使用错误版本的 Java 时立即向用户显示正确的错误消息?
NOTE: There is NOJava? source code to compile here. I onlyhave Scala source code. The Scala code requires an import java.nio.file.Path
that's available from Java 7.
注意:没有Java?源代码在这里编译。我只有Scala 源代码。Scala 代码需要import java.nio.file.Path
可从 Java 7 获得的 。
回答by Andrew Jones
In order to compile in Java 7, you should add the javac option to compile with source 1.7.
为了在 Java 7 中进行编译,您应该添加 javac 选项以使用源 1.7 进行编译。
You should add javacOptions ++= Seq("-source", "1.7")
to your SBT build config that can be found in the /project folder.
您应该添加javacOptions ++= Seq("-source", "1.7")
到可以在 /project 文件夹中找到的 SBT 构建配置。
Here's the reference from SBT: http://www.scala-sbt.org/release/docs/Detailed-Topics/Java-Sources.html
这是 SBT 的参考:http: //www.scala-sbt.org/release/docs/Detailed-Topics/Java-Sources.html
回答by Mark Harrah
Being Scala code, you can put assertions in the build definition. sbt defines the initialize
as a common place for things like this, but you can use any setting, including a custom one. For example,
作为 Scala 代码,您可以在构建定义中放置断言。sbt 将 定义initialize
为此类事情的公共场所,但您可以使用任何设置,包括自定义设置。例如,
initialize := {
val _ = initialize.value // run the previous initialization
val classVersion = sys.props("java.class.version")
val specVersion = sys.props("java.specification.version")
assert(..., "Java N or above required")
}
回答by Schleichardt
Using javacOptions ++= Seq("-source", "1.7", "-target", "1.7")
does not work if you have no Java sources.
javacOptions ++= Seq("-source", "1.7", "-target", "1.7")
如果您没有 Java 源代码,则无法使用。
But you can set the target JVM for the Scala compiler in build.sbt or Build.scala:
但是您可以在 build.sbt 或 Build.scala 中为 Scala 编译器设置目标 JVM:
scalacOptions += "-target:jvm-1.7"
As a result it prints on a JDK 6:
结果它打印在 JDK 6 上:
$ sbt clean run
[info] Set current project to default-cd5534 (in build file:/tmp/so/)
[success] Total time: 0 s, completed 27.10.2013 14:31:43
[info] Updating {file:/tmp/so/}default-cd5534...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /tmp/so/target/scala-2.10/classes...
[info] Running Main
[error] (run-main) java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 51.0
java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.accessinitialize := {
val _ = initialize.value // run the previous initialization
val required = VersionNumber("1.8")
val curr = VersionNumber(sys.props("java.specification.version"))
assert(CompatibleJavaVersion(curr, required), s"Java $required or above required")
}
...
/** Java specification version compatibility rule. */
object CompatibleJavaVersion extends VersionNumberCompatibility {
def name = "Java specification compatibility"
def isCompatible(current: VersionNumber, required: VersionNumber) =
current.numbers.zip(required.numbers).foldRight(required.numbers.size<=current.??numbers.size)((a,b) => (a._1 > a._2) || (a._1==a._2 && b))
def apply(current: VersionNumber, required: VersionNumber) = isCompatible(current, required)
}
0(URLClassLoader.java:73)
at java.net.URLClassLoader.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at java.lang.ClassLoader.loadClass(ClassLoader.java:314)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 4 s, completed 27.10.2013 14:31:47
Note: Maybe it works only for the latest SBT/Scalac version.
注意:也许它只适用于最新的 SBT/Scalac 版本。
回答by metasim
In SBT 0.13.6 there is a new VersionNumber
class and VersionNumberCompatibility
trait. Tweaking the approach recommended by @MarkHarrah to use this one might do the following:
在 SBT 0.13.6 中有一个新的VersionNumber
类和VersionNumberCompatibility
特性。调整@MarkHarrah 推荐的方法以使用此方法可能会执行以下操作:
initialize := {
val _ = initialize.value // run the previous initialization
val required = "1.8"
val current = sys.props("java.specification.version")
assert(current == required, s"Unsupported JDK: java.specification.version $current != $required")
}
回答by user2592126
For anybody in the future, this is also a good way to do it. It halts execution immediately if it cannot find the right Java version:
对于未来的任何人来说,这也是一个很好的方法。如果找不到正确的 Java 版本,它会立即停止执行:
##代码##You put this in your build.sbt
.
你把它放在你的build.sbt
.