private static String getVersionFromPom ( ) { final String absolutePath = new File ( BuildVersion . class . getResource ( BuildVersion . class . getSimpleName ( ) + ".class" ) . getPath ( ) ) . getParentFile ( ) . getParentFile ( ) . getParentFile ( ) . getParentFile ( ) . getParentFile ( ) . getParentFile ( ) . getParentFile ( ) . getAbsolutePath ( ) ; final File file = new File ( absolutePath + "/pom.xml" ) ; try ( InputStreamReader reader = new InputStreamReader ( new FileInputStream ( file ) , StandardCharsets . UTF_8 ) ) { final MavenXpp3Reader xpp3Reader = new MavenXpp3Reader ( ) ; Model model = xpp3Reader . read ( reader ) ; return model . getVersion ( ) ; } catch ( NoClassDefFoundError e ) { // if you want to get the version possibly in development add in to your pom // pax-url-aether.jar return null ; } catch ( Exception e ) { return null ; } }
reads the pom file to get this version only to be used for development or within the IDE .