Sunday, November 6, 2011

Adding a P2 Repository Dynamically at Runtime

After experiencing the pains of dealing with a homegrown thick-client update system, we decided to give Eclipse's P2 provisioning system a try. For Eclipse 3.7 (Indigo), the P2 API received quite a bit of TLC, especially on the UI end, so we felt like it was mature enough for us to take a look at. It was very easy to create an Eclipse Plugin/Feature/Product that gets updated from a static P2 repository with a URI known at build time. All you have to do is create a p2.inf file and populate it with the correct repository locations, as explained in step 7 of Ralf Eberts excellent tutorial. We, however, had a situation where each customer would have their own private repository, so we had to set the update location dynamically at run time. Here's how we did it:

First, we had to obtain the P2 provisioning agent:



Once we have the provisioning agent, we can use it to get the metadata manager and artifact manager. You need both managers because the equinox provisioning systems allows for separate locations for each. Once we have the managers, we can simply add the URIs that we retrieved at run time via some external property file, database, preference store, etc.:




You'll need to be sure and add the following imports:

  • import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
  • import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
  • import org.eclipse.equinox.internal.p2.ui.ProvUI;
  • import org.eclipse.equinox.p2.core.IProvisioningAgent;
  • import org.eclipse.equinox.p2.core.ProvisionException;
  • import org.eclipse.equinox.p2.operations.ProvisioningJob;
  • import org.eclipse.equinox.p2.operations.ProvisioningSession;
  • import org.eclipse.equinox.p2.operations.UpdateOperation;
  • import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
  • import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;

and you'll also need to make sure your plugin has the following dependencies:


  • org.eclipse.equinox.p2.core
  • org.eclipse.equinox.p2.operations
  • org.eclipse.equinox.p2.metadata
  • org.eclipse.equinox.p2.engine
  • org.eclipse.equinox.p2.ui
  • org.eclipse.equinox.p2.metadata.repository
  • org.eclipse.equinox.p2.repository