简体   繁体   English

如何在运行时为Scala.js和JVM确定Scala版本?

[英]How to determine Scala version at run-time for both Scala.js and JVM?

This question is similar to How do I get the Scala version from within Scala itself? 这个问题类似于如何从Scala本身获取Scala版本? , except I want to support Scala.js as well. ,除了我也想支持Scala.js。

I tried the util.Properties approach. 我尝试了util.Properties方法。 Unfortunately it is not available on Scala.js: 不幸的是,它在Scala.js上不可用:

https://scalafiddle.io/sf/dzNAhWB/0 https://scalafiddle.io/sf/dzNAhWB/0

ScalaFiddle.scala:-5: ERROR: There were linking errors
Referring to non-existent class java.util.jar.Attributes$Name
  called from scala.util.Properties$.()
  called from ScalaFiddle$.()
  exported to JavaScript with @JSExport
Referring to non-existent method java.util.Properties.load(java.io.InputStream)scala.Unit
  called from scala.util.PropertiesTrait.$$anonfun$scalaProps$1(java.util.Properties,java.io.InputStream)scala.Unit
  called from scala.util.Properties$.$$anonfun$scalaProps$1(java.util.Properties,java.io.InputStream)scala.Unit
  called from scala.util.PropertiesTrait.scalaProps()java.util.Properties
  called from scala.util.Properties$.scalaProps$lzycompute()java.util.Properties
  called from scala.util.Properties$.scalaProps()java.util.Properties
  called from scala.util.PropertiesTrait.scalaPropOrNone(java.lang.String)scala.Option
  called from scala.util.Properties$.scalaPropOrNone(java.lang.String)scala.Option
  called from scala.util.PropertiesTrait.$$init$()scala.Unit
  called from scala.util.Properties$.()
  called from ScalaFiddle$.()
  exported to JavaScript with @JSExport
involving instantiated classes:
  scala.util.Properties$
Referring to non-existent method java.lang.Class.getResourceAsStream(java.lang.String)java.io.InputStream
  called from scala.util.PropertiesTrait.scalaProps()java.util.Properties
  called from scala.util.Properties$.scalaProps$lzycompute()java.util.Properties
  called from scala.util.Properties$.scalaProps()java.util.Properties
  called from scala.util.PropertiesTrait.scalaPropOrNone(java.lang.String)scala.Option
  called from scala.util.Properties$.scalaPropOrNone(java.lang.String)scala.Option
  called from scala.util.PropertiesTrait.$$init$()scala.Unit
  called from scala.util.Properties$.()
  called from ScalaFiddle$.()
  exported to JavaScript with @JSExport
involving instantiated classes:
  scala.util.Properties$
Referring to non-existent method java.util.jar.Attributes$Name.(java.lang.String)
  called from scala.util.Properties$.()
  called from ScalaFiddle$.()
  exported to JavaScript with @JSExport

So, how to determine Scala version at run-time for all Scala backends? 那么,如何在运行时为所有Scala后端确定Scala版本?

I'm afraid there is no way to do that in Scala.js to begin with, let alone a platform-independent solution. 恐怕Scala.js首先无法做到这一点,更不用说平台无关的解决方案了。

What you can do is write a small task in your build tool (sbt?) that will generate a small source file encoding the information. 您可以做的是在构建工具(sbt?)中编写一个小任务,该任务将生成一个对信息进行编码的小源文件。

For example, paraphrasing the sbt documentation on source generators : 例如, 对源生成器上sbt文档进行释义:

sourceGenerators in Compile += Def.task {
  val file = (sourceManaged in Compile).value / "scalaversion" / "ScalaVersion.scala"
  val scalaV = scalaVersion.value
  IO.write(file,
      s"""
        object ScalaVersion {
          val Full = "$scalaV"
        }
      """)
  Seq(file)
}.taskValue

You can then read ScalaVersion.Full from your code. 然后,您可以从代码中读取ScalaVersion.Full

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

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