开发者

Versions in Java

开发者 https://www.devze.com 2023-01-25 20:31 出处:网络
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.

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 po开发者_运维问答m.xml? I'd also be interested to know how it's usually done with Ant.


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.


Basic solution: Maven writes version info to Manifest.xml inside jar. Read this jar from Java and extract this info.

  1. Force maven to create a more detailed Manifest.

  2. Read jar file from java:

    Manifest manifest = new JarFile("path/to/your.jar").getManifest();
    String version = manifest.getAttributes().getValue("Specification-Version:");
    
  3. If you need to find a location of your jar:

    MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消