如何在运行时获取自定义 Eclipse 功能的版本号?

发布于 2024-07-18 09:04:09 字数 71 浏览 3 评论 0原文

我想在其透视图的标题栏中显示我正在开发的自定义 Eclipse 功能的版本号。 有没有办法从运行时插件和/或工作台获取版本号?

I would like to display the version number of a custom Eclipse feature I am developing in the title bar of its perspective. Is there a way to obtain the version number from the runtime plugin and/or workbench?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不知在何时 2024-07-25 09:04:09

比如:

Platform.getBundle("my.feature.id").getHeaders().get("Bundle-Version");

应该能解决问题。

请注意(来自此线程)它不能在任何地方使用插件本身内:
this.getBundle() 在您的插件上调用 super.start(BundleContext) 后才有效。
因此,如果您在调用 super.start() 之前在构造函数中或 start(BundleContext) 中使用 this.getBundle() ,那么它将返回 null。


如果失败,您可以在这里获得更完整的“版本” :

public static String getPlatformVersion() {
  String version = null;

  try {
    Dictionary dictionary = 
      org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getBundle().getHeaders();
    version = (String) dictionary.get("Bundle-Version"); //$NON-NLS-1$
  } catch (NoClassDefFoundError e) {
    version = getProductVersion();
  }

  return version;
}

public static String getProductVersion() {
  String version = null;

  try {
    // this approach fails in "Rational Application Developer 6.0.1"
    IProduct product = Platform.getProduct();
    String aboutText = product.getProperty("aboutText"); //$NON-NLS-1$

    String pattern = "Version: (.*)\n"; //$NON-NLS-1$
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(aboutText);
    boolean found = m.find();

    if (found) {
      version = m.group(1);
    }
  } catch (Exception e) {

  }

  return version;
}

Something like:

Platform.getBundle("my.feature.id").getHeaders().get("Bundle-Version");

should do the trick.

Note (from this thread) that it can not be used anywhere within the plugin itself:
this.getBundle() is not valid until AFTER super.start(BundleContext) has been called on your plugin.
So if you are using this.getBundle() within your constructor or within your start(BundleContext) before calling super.start() then it will return null.


If that fails, you have here a more complete "version":

public static String getPlatformVersion() {
  String version = null;

  try {
    Dictionary dictionary = 
      org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getBundle().getHeaders();
    version = (String) dictionary.get("Bundle-Version"); //$NON-NLS-1$
  } catch (NoClassDefFoundError e) {
    version = getProductVersion();
  }

  return version;
}

public static String getProductVersion() {
  String version = null;

  try {
    // this approach fails in "Rational Application Developer 6.0.1"
    IProduct product = Platform.getProduct();
    String aboutText = product.getProperty("aboutText"); //$NON-NLS-1$

    String pattern = "Version: (.*)\n"; //$NON-NLS-1$
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(aboutText);
    boolean found = m.find();

    if (found) {
      version = m.group(1);
    }
  } catch (Exception e) {

  }

  return version;
}
柠檬 2024-07-25 09:04:09

我使用第一个选项:

protected void fillStatusLine(IStatusLineManager statusLine) {
    statusItem = new StatusLineContributionItem("LastModificationDate"); //$NON-NLS-1$
    statusItem.setText("Ultima Actualizaci\u00f3n: "); //$NON-NLS-1$
    statusLine.add(statusItem);
    Dictionary<String, String> directory = Platform.getBundle("ar.com.cse.balanza.core").getHeaders();
    String version = directory.get("Bundle-Version");

    statusItem = new StatusLineContributionItem("CopyRight"); //$NON-NLS-1$
    statusItem.setText(Messages.AppActionBar_18);
    statusLine.add(statusItem);
}

I use the first option:

protected void fillStatusLine(IStatusLineManager statusLine) {
    statusItem = new StatusLineContributionItem("LastModificationDate"); //$NON-NLS-1$
    statusItem.setText("Ultima Actualizaci\u00f3n: "); //$NON-NLS-1$
    statusLine.add(statusItem);
    Dictionary<String, String> directory = Platform.getBundle("ar.com.cse.balanza.core").getHeaders();
    String version = directory.get("Bundle-Version");

    statusItem = new StatusLineContributionItem("CopyRight"); //$NON-NLS-1$
    statusItem.setText(Messages.AppActionBar_18);
    statusLine.add(statusItem);
}
油饼 2024-07-25 09:04:09

正如 @zvikico 上面所说,接受的答案不适用于功能,仅适用于插件(OSGi 捆绑包,功能不适用于)。 获取有关已安装功能的信息的方法是通过 org.eclipse.core.runtime.Platform.getBundleGroupProviders() 作为 此处描述

As @zvikico says above, the accepted answer does not work for Features, only Plug-ins (OSGi Bundles, which Features are not). The way to get info about installed features is via org.eclipse.core.runtime.Platform.getBundleGroupProviders() as described here.

新一帅帅 2024-07-25 09:04:09

VonC 提供的用于检索主 Eclipse 版本号的版本,但不引用内部类(您应该避免这样做):

Platform.getBundle(PlatformUI.PLUGIN_ID).getHeaders().get("Bundle-Version");

A version of what VonC provided to retrieve the primary Eclipse version number, but one that doesn't reference internal classes (which you should avoid doing):

Platform.getBundle(PlatformUI.PLUGIN_ID).getHeaders().get("Bundle-Version");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文