13

In Eclipse plugin development: How to get current bundle version?

It is just in Manifest.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Nodeclipse
Bundle-SymbolicName: org.nodeclipse.ui;singleton:=true
Bundle-Version: 0.6.0.qualifier
Bundle-Activator: org.nodeclipse.ui.Activator
Require-Bundle: org.eclipse.ui,

However Java has method only to look at Bundle Implementation version getClass().getPackage().getImplementationVersion();

Paul Verest
  • 60,022
  • 51
  • 208
  • 332

2 Answers2

25

In a more OSGi way, not having to know your name, and official standard way:

 Version version = FrameworkUtil.getBundle(getClass()).getVersion();

Notice that the bundle version you get is from the bundle from which the this was loaded. So do not put this in a convenience library in another bundle!

Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
15

In a plug-in you can use:

Bundle bundle = Platform.getBundle("org.nodeeclipse.ui");
Version version = bundle.getVersion();

Version has getMajor, getMinor, getMicro, getQualifier methods.

Platform is org.eclipse.core.runtime.Platform

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • I think this is too brittle. If the bundle name changes, this code will no longer work. – Raffi Khatchadourian Aug 11 '15 at 23:25
  • 3
    @RaffiKhatchadourian If you want the bundle you are currently running in then, yes, use FrameworkUtil.getBundle. However when you are doing things like processing extension point definitions all you have is the plugin id so in that case use this method. – greg-449 Aug 12 '15 at 07:05