Configure a p2 update repository programmatically

前提是你 提交于 2019-12-05 22:21:21

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).

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!