Configure a p2 update repository programmatically

2019-05-16 11:30发布

There is an article in the Eclipse wiki how to configure user's p2 default repositories of an RCP application by adding a static conf file to your product:

Equinox/p2/Adding Self-Update to an RCP Application - Configuring the user's default repositories

I want to do the same programmatically in a Java class when the user changes some configuration details. I could not find appropriate p2 API documentation for that.

4条回答
forever°为你锁心
2楼-- · 2019-05-16 12:00

This is high on the Google query for this issue, and there's still not a good way to do it published:

If anyone finds this page via Google as I did, I've solved this problem. You can use org.eclipse.equinox.internal.p2.ui.model.ElementUtils.updateRepositoryUsingElements to set the repositories programmatically. Full code can be found here.

查看更多
够拽才男人
3楼-- · 2019-05-16 12:04

Furthermore you can add more than one repositories with ElementUtils and also you can sort them.

MetadataRepositoryElement[] element = new MetadataRepositoryElement[links.length];
    for (int i = 0; i < links.length; i++) {
        element[i] = new MetadataRepositoryElement(null, new URI(links[i]), true);
        element[i].setNickname("Link-"+i);
    }
    ElementUtils.updateRepositoryUsingElements(element, null);

These links will be appeared alphabetically sorted.

查看更多
何必那么认真
4楼-- · 2019-05-16 12:19

Use this solution for Eclipse 3.7 based applications:

final ProvisioningUI ui = ProvUIActivator.getDefault().getProvisioningUI();
IArtifactRepositoryManager artifactManager = ProvUI.getArtifactRepositoryManager(ui.getSession());
artifactManager.addRepository(new URI(UPDATE_SITE_URL);

IMetadataRepositoryManager metadataManager = ProvUI.getMetadataRepositoryManager(ui.getSession());
metadataManager.addRepository(new URI(UPDATE_SITE_URL);

For ProvUI and ProvisioningUI you have to import bundles org.eclipse.equinox.p2.ui and org.eclipse.equinox.p2.operations (among others).

查看更多
beautiful°
5楼-- · 2019-05-16 12:26

I found a solution. It's easy - unfortunately there is no documentation...

    // from bundle org.eclipse.equinox.p2.console
    import org.eclipse.equinox.internal.p2.console.ProvisioningHelper;

    URI repoUri = new URI(UPDATE_SITE_URL);
    try {
        ProvisioningHelper.addMetadataRepository(repoUri);         
    } catch( Exception e ) {
        LOG.warn("Can not add update repository: " + repoUri);           
    }
    try {
        ProvisioningHelper.addArtifactRepository(repoUri);          
    } catch( Exception e ) {
        LOG.warn("Can not add update repository: " + repoUri);
    }
查看更多
登录 后发表回答