简体   繁体   English

是否在sbt中为Scala项目执行Java版本?

[英]Enforcing Java version for Scala project in sbt?

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 NO Java™ source code to compile here. 注意: 没有 Java™源代码可在此处进行编译。 I only have Scala source code. 只有 Scala源代码。 The Scala code requires an import java.nio.file.Path that's available from Java 7. Scala代码需要Java 7中提供的import java.nio.file.Path

Using javacOptions ++= Seq("-source", "1.7", "-target", "1.7") does not work if you have no Java sources. 如果没有Java源,则使用javacOptions ++= Seq("-source", "1.7", "-target", "1.7")无效。

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.access$000(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.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版本。

Being Scala code, you can put assertions in the build definition. 作为Scala代码,您可以将断言放在构建定义中。 sbt defines the initialize as a common place for things like this, but you can use any setting, including a custom one. sbt将initialize定义为此类事情的常用位置,但是您可以使用任何设置,包括自定义设置。 For example, 例如,

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")
}

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版本,它将立即停止执行:

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")
}

You put this in your build.sbt . 您将其放在build.sbt

In SBT 0.13.6 there is a new VersionNumber class and VersionNumberCompatibility trait. 在SBT 0.13.6中,有一个新的VersionNumber类和VersionNumberCompatibility特性。 Tweaking the approach recommended by @MarkHarrah to use this one might do the following: 调整@MarkHarrah建议使用的方法可能会执行以下操作:

initialize := {
    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)
}

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

Just in-case if you use eclipse based scala-ide change settings in 万一如果您在中使用基于Eclipse的Scala-ide更改设置,

window --> pref -- scala compiler --> standard --> target --> jvm-1.7 窗口->首选项-Scala编译器->标准->目标-> jvm-1.7

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM