public static String getArtifactVersion ( final String groupId , final String artifactId ) { final Properties dependencies = new Properties ( ) ; InputStream depInputStream = null ; try { String depFilePath = "META-INF/maven/dependencies.properties" ; URL fileURL = MavenUtils . class . getClassLoader ( ) . getResource ( depFilePath ) ; if ( fileURL == null ) { // try the TCCL for getResource fileURL = Thread . currentThread ( ) . getContextClassLoader ( ) . getResource ( depFilePath ) ; } if ( fileURL == null ) { throw new FileNotFoundException ( "File [" + depFilePath + "] could not be found in classpath" ) ; } depInputStream = fileURL . openStream ( ) ; dependencies . load ( depInputStream ) ; final String version = dependencies . getProperty ( groupId + "/" + artifactId + "/version" ) ; if ( version == null ) { throw new IllegalArgumentException ( "Could not resolve version. Do you have a dependency for " + groupId + "/" + artifactId + " in your maven project?" ) ; } return version ; } catch ( IOException e ) { throw new IllegalArgumentException ( "Could not resolve version for groupId:" + groupId + " artifactId:" + artifactId + " by reading the dependency information generated by maven." , e ) ; } finally { if ( depInputStream != null ) { try { depInputStream . close ( ) ; } catch ( IOException e ) { // ignore exceptions on close. } } } }
Gets the artifact version out of dependencies file . The dependencies file had to be generated by using the maven plugin .