简体   繁体   中英

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? , except I want to support Scala.js as well.

I tried the util.Properties approach. Unfortunately it is not available on Scala.js:

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?

I'm afraid there is no way to do that in Scala.js to begin with, let alone a platform-independent solution.

What you can do is write a small task in your build tool (sbt?) that will generate a small source file encoding the information.

For example, paraphrasing the sbt documentation on source generators :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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