Created
April 18, 2017 17:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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