I have noticed that in a Maven artifact’s JAR, the project.version attribute is included in two files:
META-INF/maven/${groupId}/${artifactId}/pom.properties
META-INF/maven/${groupId}/${artifactId}/pom.xml
Is there a recommended way to read this version at runtime?
You should not need to access Maven-specific files to get the version information of any given library/class.
You can simply use
getClass().getPackage().getImplementationVersion()to get the version information that is stored in a .jar-filesMANIFEST.MF. Unfortunately Maven does not write the correct information to the manifest as well by default!Instead one has to modify the
<archive>configuration element of themaven-jar-pluginto setaddDefaultImplementationEntriesandaddDefaultSpecificationEntriestotrue, like this:Ideally this configuration should be put into the company
pomor another base-pom.Detailed documentation of the
<archive>element can be found in the Maven Archive documentation.