【问题标题】:How can I obtain the version number of a custom Eclipse feature at runtime?如何在运行时获取自定义 Eclipse 功能的版本号?
【发布时间】:2010-10-23 11:17:13
【问题描述】:

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

【问题讨论】:

    标签: eclipse eclipse-plugin version eclipse-pde


    【解决方案1】:

    类似:

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

    应该可以解决问题。

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


    如果失败,这里有一个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;
    }
    

    【讨论】:

    • AFAIK,这适用于插件,但不适用于功能。我不确定是否有办法获取功能版本。
    【解决方案2】:

    我使用第一个选项:

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

    【讨论】:

    • 更好:String version = Platform.getBundle("ar.com.cse.balanza.core").getVersion().toString();
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-13
        • 2016-02-12
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2019-12-21
        相关资源
        最近更新 更多