0

I am unable to get the version at all when the JAR is already running. It always Returns null. I was thinking that Maven automatically sets the Manifest file for me so I don't have to manually create it myself in the Resources folder.

pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AutomationTool</groupId>
<artifactId>Automation</artifactId>
<version>1.2.0</version>
<packaging>jar</packaging>
<name>Automation Tool</name>
<dependencies>
    ...
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
        </plugin>
    </plugins>
</build>

My Code to get the Version from pom.xml

        JarFile jarFile;
    try {
        jarFile = new JarFile(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        Manifest manifest = jarFile.getManifest();
        Attributes attr = manifest.getMainAttributes();

        version = attr.getValue("Implementation-Version");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
sander126
  • 35
  • 7
  • 1
    **Caution:** Your code *will eventually fail,* for two reasons: first, [getCodeSource() can return null](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/security/ProtectionDomain.html#getCodeSource()); second, the getPath method of URL *does not return a valid file name.* Unlike a file path, a URL’s path is not allowed to contain certain characters, so those characters will be percent-escaped. The proper way to read the implementation version is `MyClass.class.getPackage().getImplementationVersion()`. – VGR Feb 13 '23 at 23:05
  • Thank you. I've tried the method you mentioned before I end up with my code above but it still does not work. I tried to extract the JAR file and looked at the Manifest File and I do not see any ImplementationVersion at all. I'm guessing Maven did not properly include the Implementation Version in the Manifest itself – sander126 Feb 14 '23 at 03:12
  • I think they are no longer added automatically and you have to do it yourself. See https://stackoverflow.com/questions/74709548/implementation-version-not-in-manifest-after-spring-boot-maven-plugin-repack and [the Maven plugin page](https://maven.apache.org/shared/maven-archiver/examples/manifest.html) to which that answer links. – VGR Feb 14 '23 at 15:17

0 Answers0