eclipsercp

How do I retrieve Eclipse RCP Product Version from IProduct


I wish to programatically determine the version of my RCP Product.

I've found the Platform.getProduct() method, which returns an IProduct object. But, this does not have version details. I can navigate into the getDefiningBundle object, but that only has the version id of the plugin project, not the version number in the .product file.

Any tips most welcome.


Solution

  • Eventually figured out you can query the p2 product IUs to retrieve the versions of installed products. I'm doing this in a custom splash handler, and have a lot of null guards in as I'm not entirely sure how much of this API will be available in development environment launch configs. You might be comfortable removing those! I think you could probably construct a P2 query for the specific product IU instead of looping through all the products like I did. Pretty sure it's also possible to obtain the active profile without using ProvisioningUI, but that was good enough for me.

    import org.eclipse.equinox.p2.core.IProvisioningAgent;
    import org.eclipse.equinox.p2.engine.IProfile;
    import org.eclipse.equinox.p2.engine.IProfileRegistry;
    import org.eclipse.equinox.p2.metadata.IInstallableUnit;
    import org.eclipse.equinox.p2.operations.ProvisioningSession;
    import org.eclipse.equinox.p2.query.IQuery;
    import org.eclipse.equinox.p2.query.IQueryResult;
    import org.eclipse.equinox.p2.query.QueryUtil;
    import org.eclipse.equinox.p2.ui.ProvisioningUI;
    
    private org.eclipse.equinox.p2.metadata.Version getProductVersion() {
        ProvisioningUI provisioningUI = ProvisioningUI.getDefaultUI();
        if (provisioningUI == null) return null;
    
        String profileId = provisioningUI.getProfileId();
        ProvisioningSession session = provisioningUI.getSession();
        if (profileId == null || session == null) return null;
    
        IProvisioningAgent provisioningAgent = session.getProvisioningAgent();
        if (provisioningAgent == null) return null;
    
        IProfileRegistry registry = (IProfileRegistry) provisioningAgent.getService(IProfileRegistry.SERVICE_NAME);
        if (registry == null) return null;
    
        IProfile profile = registry.getProfile(profileId);
        if (profile == null) return null;
    
        IQuery<IInstallableUnit> iuProductQuery = QueryUtil.createIUProductQuery();
        IQueryResult<IInstallableUnit> result = profile.query(iuProductQuery, null);
        if (result == null) return null;
    
        for (IInstallableUnit productIU : result) {
            if (PRODUCT_ID.equals(productIU.getId())) {
                return productIU.getVersion();
            }
        }
    
        return null;
    }