Skip to content

Instantly share code, notes, and snippets.

@romanowski
Last active October 7, 2021 22:34
Show Gist options
  • Save romanowski/de14691cab7340134e197419bc48919a to your computer and use it in GitHub Desktop.
Save romanowski/de14691cab7340134e197419bc48919a to your computer and use it in GitHub Desktop.
Get currently used Scala and Java versions. Works with 2.12, 2.13 and 3.x. Based on https://gist.github.com/ymasory/923640/131ba9b367e4ea111e35ac1d57bf1279596a0439
object ScalaVersion extends App {
def props(url: java.net.URL): java.util.Properties = {
val properties = new java.util.Properties()
val is = url.openStream()
try {
properties.load(is)
properties
} finally is.close()
}
def scala2Version: String =
props(getClass.getResource("/library.properties")).getProperty("version.number")
def checkScala3(res: java.util.Enumeration[java.net.URL]): String =
if (!res.hasMoreElements) scala2Version else {
val manifest = props(res.nextElement)
manifest.getProperty("Specification-Title") match {
case "scala3-library-bootstrapped" =>
manifest.getProperty("Implementation-Version")
case _ => checkScala3(res)
}
}
val manifests = getClass.getClassLoader.getResources("META-INF/MANIFEST.MF")
val scalaVersion = checkScala3(manifests)
val javaVersion = System.getProperty("java.version")
println(s"Scala: $scalaVersion Java: $javaVersion")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment