How to get package version at running Tomcat?
I try getClass().getPackage().getImplementationVersion() but this always returns null
.
I guess it is because my war wasn't yet packaged and Tomcat executes .classes (aka exploded war).
META-INF\MANIFEST.MF
is present in final war, but not in target\<project.name>\META-INF
folder
Package objects contain version information about the implementation and specification of a Java package. This versioning information is retrieved and made available by the ClassLoader instance that loaded the class(es). Typically, it is stored in the manifest that is distributed with the classes.
Related is Get Maven artifact version at runtime
UPDATE. Before I have already added configuration for war build. But when running Tomcat from Eclipse, I get null
.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
<!-- this actually moves classes from \WEB-INF\classes to new jar
<archiveClasses>true</archiveClasses>
-->
</configuration>
</plugin>
maven-jar-plugin
configuration in pom.xml: https://mcmap.net/q/100548/-get-maven-artifact-version-at-runtime – Moeller