Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package.getImplementationVersion() returns NULL

Thanks in advance for any help.

What I want to achieve is project version (Implementation-Version) is printed in a class when it is initiated so that I can trace the version from log file.

I build a JAR file containing the following classes:

com.company.core.common.ClassA
com.company.core.security.ClassB
com.company.core.sql.ClassC

In constructor of com.company.core.sql.ClassC, I want to call System.out.println() to print out the Implementation-Version stored in META-INF/MANIFEST.MF file by calling this.class.getPackage().getImplementationVersion(), but it is getting null value when the class is initiated via Maven test class.

My MANIFEST.MF file contains the following details:

Manifest-Version: 1.0
Implementation-Title: Company Core Library
Implementation-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: simon
Implementation-Vendor-Id: com.company.core
Build-Time: 2017-02-18T15:07:33Z
Class-Path: lib/sqljdbc42-4.2.jar lib/log4j-api-2.7.jar lib/log4j-core
 -2.7.jar lib/json-20160810.jar lib/junit-4.12.jar lib/hamcrest-core-1
 .3.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_102
Implementation-Vendor: Company

Name: com/company/core/
Implementation-Vendor-Id: com.company.core
Implementation-Title: Company Core Library
Implementation-Version: 1.0
Implementation-Vendor: Company

Would I be able to get the Implementation-Version in Maven test class? If yes, what have I missed out here?

Thanks.

Regards, Simon.

like image 696
Simon Tneoh Avatar asked Apr 08 '26 17:04

Simon Tneoh


1 Answers

Within the development environment all class files are found in a named folder target (e.g. within Eclipse). The package path is represented as a directory structure. For a directory structure the existance of a Manifest is not defined. Therefore the dedicated class loader will ignore all requests for a Manifest. This is the reason the call returns null.

Running in the productive environment your product is packed within a jar-file. In this case it is defined, that there is a Manifest-file. The jar file class loader knows about the manifest file and will display version information correctly.

What helped us was to provide a default of "Implementation Version" if the version is retrieved within the development environment.

like image 100
Matthias Brenner Avatar answered Apr 11 '26 05:04

Matthias Brenner