p2 Publisher — Part II

Last week I presented the publishe, the tool used to put your deployable entities (e.g., bundles) into a p2 repository.  The publisher can be used in 4 different ways

  1. As a set of headless Eclipse applications;
  2. As a set of Ant tasks;
  3. Through its extensible API; and
  4. Within PDE Build

Last week I gave some examples of the first two uses. Today I am going to demonstrate how the publisher can be invoked programatically.  Before I continue, I should point out that the publisher has no official API.  It contains provisional API, which we will hopefully solidify, but there is a chance it might change.  While some might shy away from the provisional API, I argue that you should embrace it.  We are very interested in hearing your usecases.  By using the publisher now, and giving the p2 team feedback, we can help you move forward if the API does change.

The publisher is structured as a series of Actions and Advice.  The publisher can be invoked as follows:

 IPublisherInfo publisherInfo = ...;
 IPublisherAction[] actions = ...;
 Publisher publisher = new Publisher( publisherInfo );
 publisher.publish( actions, progressMonitor );

In this case we create a PublisherInfo to hold the context for this particular publisher request. We also create a series of actions to invoke. Finally we create and invoke the publisher.

PublisherInfo
The PublisherInfo provides a context to use when publishing. The publisher info object describes such things as:

  • The metadata repository where the IUs should be published
  • The artifact repository where the artifacts should be published
  • Artifact publishing options (overwrite existing artifacts, etc…)

Publisher Actions
There are a number of pre-defined actions for publishing well known constructs such as BundlesAction, FeaturesAction, SiteXMLAction, etc… In addition to the provided actions, custom actions can be created by extending AbstractPublisherAction.

(Tip: To see all available Actions, open the AbstractPublisherAction and press F4. This will open the hierarchy view and show you all the classes that extend AbstractPublisherAction).

Example
The following shows how you can create a publisher application that just publishes bundles. There are a few paths defined as constants at the top of the file.

...
 /**
  * This simple publisher example demonstrates how to use the publisher
  * API to publish a list of bundles.
  * @throws URISyntaxException
  * @throws ProvisionException
  */
 public class PublisherExample implements IApplication {
 
	public static String BUNDLE_LOCATION = "/home/mybundles"; // Set this to the location of your bundles
	public static String ARTIFACT_REPOSITORY = "file:/artifact_repo"; // Set this to your artifact repository
	public static String METADATA_REPOSITORY = "file:/metadata_repo"; // Set this to your metadata repository
 
	public Object start(IApplicationContext context) throws Exception {
		IPublisherInfo info = createPublisherInfo();
		IPublisherAction[] actions = createActions();
		Publisher publisher = new Publisher(info);
		publisher.publish(actions, new NullProgressMonitor());
		return IApplication.EXIT_OK;
	}
 
	public IPublisherInfo createPublisherInfo() throws ProvisionException, URISyntaxException {
		PublisherInfo result = new PublisherInfo();
 
		// Create the metadata repository.  This will fail if a repository already exists here
		IMetadataRepository metadataRepository = new SimpleMetadataRepositoryFactory().create(new URI(METADATA_REPOSITORY), 
"Sample Metadata Repository", 
MetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, 
Collections.EMPTY_MAP);
 
		// Create the artifact repository.  This will fail if a repository already exists here
		IArtifactRepository artifactRepository = new SimpleArtifactRepositoryFactory().create(new URI(ARTIFACT_REPOSITORY), 
"Sample Artifact Repository", 
ArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, 
Collections.EMPTY_MAP);
 
		result.setMetadataRepository(metadataRepository);
		result.setArtifactRepository(artifactRepository);
		result.setArtifactOptions(IPublisherInfo.A_PUBLISH | IPublisherInfo.A_INDEX);
		return result;
	}
 
	public IPublisherAction[] createActions() {
		IPublisherAction[] result = new IPublisherAction[1];
		File[] bundleLocations = new File[1];
		bundleLocations[0] =  new File(BUNDLE_LOCATION);
		BundlesAction bundlesAction = new BundlesAction(bundleLocations);
		result[0] = bundlesAction;
		return result;
	}
 }
...

To help get you started, I have attached the sample code as an Eclipse project

Happy Publishing!

Comments are closed.

No responses yet

Written by . Published in Categories: Planet Eclipse