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();
}