Reputation: 723
I have to manage a Java build, and I don't know how to best manage the software's version so that it can be displayed on an about dialog etc.
How do you people do it? We are using Maven, can I somehow reuse the version as defined in pom.xml? I'd also be interested to know how it's usually done with Ant.
Upvotes: 4
Views: 238
Reputation: 80330
Basic solution: Maven writes version info to Manifest.xml inside jar. Read this jar from Java and extract this info.
Force maven to create a more detailed Manifest.
Read jar file from java:
Manifest manifest = new JarFile("path/to/your.jar").getManifest();
String version = manifest.getAttributes().getValue("Specification-Version:");
If you need to find a location of your jar:
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Upvotes: 3
Reputation: 8949
You can get the Maven version of the jar by loading the pom.properties file. This is automatically created by Maven in the jars.
Should be located inside the jar, at this path
META-INF\maven\{groupId}\{artifactId}\pom.properties
This is mentioned a little bit more here.
Upvotes: 5
Reputation: 2879
We have a large development team so it's more useful for us to know exactly which build from a given target release we're running or talking about. This is why you'll see people include the build number in their version information. Personally, it's all an arbitrary label and the time stamp of the build in YYYYMMddHHmmss (or similar) format provides a nice, naturally sortable version.
The maven-resource-plugin allows you to define a filter string to replace with a value when it builds your JAR files. Then just read the value like any other application property.
Upvotes: 1