public static synchronized String getVersion ( ) { // Try to get version number from pom.xml (available when running in Eclipse) final Class < ? > cls = ClassGraph . class ; try { final String className = cls . getName ( ) ; final URL classpathResource = cls . getResource ( "/" + JarUtils . classNameToClassfilePath ( className ) ) ; if ( classpathResource != null ) { final Path absolutePackagePath = Paths . get ( classpathResource . toURI ( ) ) . getParent ( ) ; final int packagePathSegments = className . length ( ) - className . replace ( "." , "" ) . length ( ) ; // Remove package segments from path Path path = absolutePackagePath ; for ( int i = 0 ; i < packagePathSegments && path != null ; i ++ ) { path = path . getParent ( ) ; } // Remove up to two more levels for "bin" or "target/classes" for ( int i = 0 ; i < 3 && path != null ; i ++ , path = path . getParent ( ) ) { final Path pom = path . resolve ( "pom.xml" ) ; try ( InputStream is = Files . newInputStream ( pom ) ) { final Document doc = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) . parse ( is ) ; doc . getDocumentElement ( ) . normalize ( ) ; String version = ( String ) XPathFactory . newInstance ( ) . newXPath ( ) . compile ( "/project/version" ) . evaluate ( doc , XPathConstants . STRING ) ; if ( version != null ) { version = version . trim ( ) ; if ( ! version . isEmpty ( ) ) { return version ; } } } catch ( final IOException e ) { // Not found } } } } catch ( final Exception e ) { // Ignore } // Try to get version number from maven properties in jar's META-INF directory try ( InputStream is = cls . getResourceAsStream ( "/META-INF/maven/" + MAVEN_PACKAGE + "/" + MAVEN_ARTIFACT + "/pom.properties" ) ) { if ( is != null ) { final Properties p = new Properties ( ) ; p . load ( is ) ; final String version = p . getProperty ( "version" , "" ) . trim ( ) ; if ( ! version . isEmpty ( ) ) { return version ; } } } catch ( final IOException e ) { // Ignore } // Fallback to using Java API (version number is obtained from MANIFEST.MF) final Package pkg = cls . getPackage ( ) ; if ( pkg != null ) { String version = pkg . getImplementationVersion ( ) ; if ( version == null ) { version = "" ; } version = version . trim ( ) ; if ( version . isEmpty ( ) ) { version = pkg . getSpecificationVersion ( ) ; if ( version == null ) { version = "" ; } version = version . trim ( ) ; } if ( ! version . isEmpty ( ) ) { return version ; } } return "unknown" ; }
Get the version number of ClassGraph .