简体   繁体   English

检测当前的应用版本

[英]Detect current app version

When I was working with XCode and iOS, there was a simple way to check the application's current version by reading the plist. 当我使用XCode和iOS时,有一种简单的方法可以通过读取plist来检查应用程序的当前版本。

Is there a similar way to do this in Java? 在Java中有类似的方法吗?

XCode stores that version value in a resource file that is distributed with your application. XCode将该版本值存储在与应用程序一起分发的资源文件中。 In Java the equivalent would be your Manifest file, which is packed inside your JAR/WAR/EAR archive. 在Java中,等效文件将是清单文件,该文件打包在JAR / WAR / EAR归档文件中。

A Manifest file is just a metadata text file named MANIFEST.MF that stores some standard key/value pairs which are recognized by many tools and that is packaged inside a special folder named META-INF inside your java archive. 清单文件只是一个名为MANIFEST.MF的元数据文本文件,该文件存储一些标准键/值对,这些对键/值对可被许多工具识别,并打包在Java归档文件中名为META-INF的特殊文件夹中。

To get the Manifest file for your own JAR this question would give you some clues. 要获取自己的JAR的清单文件, 此问题将为您提供一些线索。 Once you have your own Manifest instance then use either one of the next options to get that version value. 拥有自己的Manifest实例后,请使用以下任一选项获取版本值。

This way to get the Specification Version: 通过这种方式获得规范版本:

Manifest mf = .... // get the manifest file
String specVersion = mf.getAttribute("Specification-Version");

This way to get the Implementation Version: 通过这种方式获得实施版本:

Manifest mf = .... // get the manifest file
String specVersion = mf.getAttribute("Implementation-Version");

More info regarding the JAR manifests can be found here . 有关JAR清单的更多信息,请参见此处

EDIT: 编辑:

If you are getting null values for any of those properties that means that they haven't been configured in your MANIGEST.MF file. 如果您获得这些属性中任何一个的null值,则意味着尚未在MANIGEST.MF文件中MANIGEST.MF进行配置。 That's easy to check: unzip your JAR file (JAR files are just ZIP files with a different extension name) and go the META-INF folder to find the MANIFEST.MF file, since it's a text file you can print its contents to the console, if there is a Specification-Version or Implementation-Version attribute defined there and you are still getting null values then you might be loading a manifest file from a different JAR. 这很容易检查:解压缩您的JAR文件(JAR文件只是具有不同扩展名的ZIP文件),然后转到META-INF文件夹中查找MANIFEST.MF文件,因为它是文本文件,所以您可以将其内容打印到控制台,如果在那里定义了Specification-VersionImplementation-Version属性,而您仍然获得null值,则可能是从其他JAR加载清单文件。

FOR THE RECORD: 作为记录:

To get that attributes in your Manifest file you would need to configure your build tool to do so. 要在清单文件中获取该属性,您需要配置构建工具来这样做。 Maven would do it automatically (you can customize it though), with Ant you will need to use a specific Ant Task , with Eclipse you will need go through its docs (same with any other IDE). Maven会自动执行此操作(尽管您可以对其进行自定义 ),而使用Ant则需要使用特定的Ant Task ,而使用Eclipse则需要阅读其文档 (与任何其他IDE相同)。

As Alonso says, in Java, your code isn't automatically assigned a build version by the compiler. 正如Alonso所说,在Java中,编译器不会自动为您的代码分配构建版本。 Java leaves that up to the build tool that your compiler is run by, eg ant or maven. Java将其留给编译器运行的构建工具,例如ant或maven。 If your app isn't using the manifest file, which is often the case, but using instead a version number suffix, eg my_app_1.2.3.jar then you could do this to get the jar name: 如果您的应用程序不使用清单文件(通常是这种情况),而是使用版本号后缀(例如my_app_1.2.3.jar),则可以这样做以获取jar名称:

Class.getProtectionDomain().getCodeSource().getLocation()

If it has a GUI and the main purpose 1 is 'update' use Java Web Start to deploy it. 如果它具有GUI且主要目的1是“更新”,请使用Java Web Start进行部署。

For displaying the version to the user I would store the version number in the manifest of each Jar of the app., and show the Implementation-Version s in a JTable at run-time. 为了向用户显示版本,我将版本号存储在应用程序的每个Jar的清单中,并在运行时在JTable中显示Implementation-Version

  1. As an aside, to get better answers, put aside what you are trying to 'do' and instead name the 'feature' you are trying to achieve. 顺便说一句,要获得更好的答案,请搁置您要“做什么”,而要命名您要实现的“功能”。 The latter can be expressed as the application feature you would like to offer the user. 后者可以表示为您想要为用户提供的应用程序功能。 It might be something like 'Can be expanded with plug-ins', 'Has free auto-upgrade for 24 months', 'Whiter, brighter, more suds'.. 它可能类似于“可以使用插件扩展”,“可以免费升级24个月”,“更白,更明亮,更泡沫”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM