How to get package version at running Tomcat?
Asked Answered
A

2

7

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

java.lang.Package

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>
Areopagus answered 18/2, 2013 at 10:38 Comment(0)
A
19

Solved by
1) Creating META-INF\MANIFEST.MF in webapp folder with mvn war:manifest
2) Coding

version  = getClass().getPackage().getImplementationVersion();  
if (version==null) {
    Properties prop = new Properties();
    try {
        prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
        version = prop.getProperty("Implementation-Version");
    } catch (IOException e) {
        logger.error(e.toString());
    }
}
logger.info("Starting App version "+version);

Thanks to @javadude answer at How do I read the manifest file for a webapp running in apache tomcat?

Areopagus answered 19/2, 2013 at 5:36 Comment(1)
A solution with maven-jar-plugin configuration in pom.xml: https://mcmap.net/q/100548/-get-maven-artifact-version-at-runtimeMoeller
T
2

The normal source of a package's version / title / vendor information is attributes in the JAR file manifest. See the JAR File Specification.

If the getters return null that means that the corresponding attribute has not been specified. (And that is normal for regular JAR files too.)

Yes, it is related to the linked question. The accepted answer describes what to do to tell Maven to add some Maven version details to a JAR manifest so that you can access them at runtime using the Package API.


How to get package version at running Tomcat?

You need to create JAR files, and put the version info into the respective JAR manifests. Apparently you can also do this at the WAR file level.

Reference: "Deploy: War File Versioning and Manifest Reader" by Fred Puls.

Trappings answered 18/2, 2013 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.