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

有帮助吗?

解决方案

类似的东西:

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

应该这样做。

注意(来自此主题)它不能可以在 in 插件本身的任何地方使用:
在您的插件上调用AFTER super.start(BundleContext)之后, this.getBundle()无效。
因此,如果您在构造函数中或在 start(BundleContext)中使用 this.getBundle(),然后再调用 super.start(),那么将返回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;
}

其他提示

我使用第一个选项:

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);
}

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

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

Platform.getBundle(PlatformUI.PLUGIN_ID).getHeaders().get("Bundle-Version");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top