0

I've been working on this for a while and tried every possible solution on here, but nothing seems to works. Apparently, I couldn't find anyone having the same problem on here as well. Please help :)

Every time I use .getPackage().getImplementationVersion(), my program is not returning my implementation version from my pom.xml or MANIFEST.MF but the java version of the running VM. This happens for both the workspace as well as the final jar. The manifest-file seems to be just fine. At first, I thought it takes the Build-Jdk from the manifest, but no, it actually dynamically gets the running java version.

I know there is an option to get the version by reading through the pom (even though I have not tried that yet), but I really would appreciate to have this code get going somehow.

I built a minimum reproducible example with JavaFX, the desired version is written into the title of the popup window.

I'm using Java Zulu 8 and Maven 3.3.9 on Eclipse Oxygen atm.

App.java

# import javafx.application.Application;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;

public class App extends Application {

    public static void main(String\[\] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage stage) throws Exception {
    
            Scene scene = new Scene(new AnchorPane());          
            stage.setScene(scene);
            stage.setTitle(App.class.getClass().getPackage().getImplementationVersion());           
            stage.show();
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.xyz.TestVersion</groupId>
    <artifactId>VersionTester</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>VersionTester</name>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- main class -->
                            <mainClass>App</mainClass>                          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>                         <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

MANIFEST.MF that is being created in the JAR-file

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.8.0_322
Specification-Title: VersionTester
Specification-Version: 0.0.1-SNAPSHOT
Implementation-Title: VersionTester
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: de.xyz.TestVersion
Main-Class: App

already tried:

  • getPackage().getImplementationVersion()

  • getPackage().getSpecificationVersion()

  • tried both in different classes in different packages of the project, ultimately in the Main-Class

  • tried maven-jar-plugin instead of maven-assembly-plugin

1 Answers1

0

"Every time I use .getPackage().getImplementationVersion(), my program is not returning my implementation version from my pom.xml or MANIFEST.MF but the java version of the running VM."

That is because this is exactly what getImplementationVersion() should do. The documentation talks about the version "of this implementation". By that the documentation means the version of the Java implementation. Every Java release has several implementations, like the Oracle JDK, OpenJDK, Eclipse, Microsoft, Google, Amazon all have their implementation. They never meant to return the version of the library, application or whatnot.

What you want to achive can be done in several ways. The most usual way is to create a version.properties file in src/main/resources and have a key

version: ${project.version}

Configure in the pom to filter this resource when it gets copied during the build from the resource directory to the target/classes directory. After that your code simply has to load this properties file and use the value.

Peter Verhas
  • 268
  • 1
  • 6
  • hey, thank you for this answer! Its a little confusing, many stack-pages state that getImplementationVersion() method equals the information from manifest.mf and manifest uses the artifact-version for this. A good example is written here: [link](https://github.com/khmarbaise/version-examples/blob/master/version-example-ii/src/main/java/com/soebes/examples/TheVersionClass.java) I thought since my manifest.mf clearly takes my artifact version (0.0.1-SNAPSHOT) this should work somehow? I've seen stacks getting the snapshot result, too. No example retrieved the java version like mine did though – Anna Pixie Mar 30 '22 at 08:27
  • 1
    The given link is nice but you have to be aware that the following configuration is needed: https://github.com/khmarbaise/version-examples/blob/master/version-example-ii/pom.xml#L28 – khmarbaise Mar 30 '22 at 13:07