Skip to content

Instantly share code, notes, and snippets.

@leifwickland
Created April 18, 2017 17:52
Show Gist options
  • Save leifwickland/2be89f98eee65e952dc3444a9a163070 to your computer and use it in GitHub Desktop.
Save leifwickland/2be89f98eee65e952dc3444a9a163070 to your computer and use it in GitHub Desktop.
Retrieves the version information which is stored in the manifest of a JAR
/**
* Reads the version number out of the manifest.
*/
object Version {
def apply: Option[String] = {
val myClass = this.getClass
val classFileName = s"${myClass.getSimpleName}.class"
val myUrl = myClass.getResource(classFileName)
val myUrlString = myUrl.toString
val lastBang = myUrlString.lastIndexOf('!')
myUrl.getProtocol match {
case "jar" if lastBang > 0 =>
val manifestPath = myUrlString.take(lastBang + 1) + "/META-INF/MANIFEST.MF"
Some(new java.util.jar.Manifest(new URL(manifestPath).openStream()).getMainAttributes.getValue("Implementation-Version"))
case _ =>
None
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment