public static Either < String , String > getBuildVersion ( ) { //Find the right manifest from the many on classpath. See http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest Class clazz = BuildNumberCommand . class ; String className = clazz . getSimpleName ( ) + ".class" ; String classPath = clazz . getResource ( className ) . toString ( ) ; if ( ! classPath . startsWith ( "jar" ) ) { return Either . right ( "Cannot determine build version when not running from JAR." ) ; } String manifestPath = classPath . substring ( 0 , classPath . lastIndexOf ( ' ' ) + 1 ) + "/META-INF/MANIFEST.MF" ; try { Manifest manifest = new Manifest ( new URL ( manifestPath ) . openStream ( ) ) ; String build = manifest . getMainAttributes ( ) . getValue ( BUILD_VERSION ) ; if ( build == null ) { return Either . right ( String . format ( "Cannot determine build version when %s missing from JAR manifest %s" , BUILD_VERSION , MANIFEST ) ) ; } else { return Either . left ( build ) ; } } catch ( IOException e ) { return Either . right ( String . format ( "Cannot access %s: %s" , manifestPath , e ) ) ; } }
Return build number as indicated by git SHA embedded in JAR MANIFEST . MF