I was looking up how to get the application name(artifact id) and version from maven pom or manifest when I came across this question Get Maven artifact version at runtime.
The above works for me when I package the project but I can't seem to get anything to work when I try to run the program using eclipse. I tried using the .properties method when building since I assumed that is not package dependent but I am still not getting a result. If anyone has an idea or solution to this problem it would be greatly appreciated.
My last attempt is below. This uses the manifest when packaged(which works) and trying to get the .properties file when running in eclipse.
String appVersion = getClass().getPackage().getImplementationVersion(); if(appVersion == null || "".equals(appVersion)) { appVersion = Glob.getString(appVersion); if(appVersion == null || "".equals(appVersion)) { System.exit(0); } }
Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time.
artifactId is the name of the jar without version. If you created it, then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar, you have to take the name of the jar as it's distributed.
Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how the project artifact should be packaged: groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project.
Create a property file
src/main/resources/project.properties
with the below content
version=${project.version} artifactId=${project.artifactId}
Now turn on maven resource filtering
<resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource>
so that this file is processed into
target/classes/project.properties
with some content similar to this
version=1.5 artifactId=my-artifact
Now you can read this property file to get what you want and this should work every time.
final Properties properties = new Properties(); properties.load(this.getClassLoader().getResourceAsStream("project.properties")); System.out.println(properties.getProperty("version")); System.out.println(properties.getProperty("artifactId"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With