You can create a P2 repo programmatically with your plugin using
extensible API of P2 publisher. P2 repo contains four items as
plugins folder,
features folder,
content.xml and
artifact.xml.
Here is the code:
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.equinox.internal.p2.artifact.repository.ArtifactRepositoryManager;
import org.eclipse.equinox.internal.p2.metadata.repository.MetadataRepositoryManager;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository;
import org.eclipse.equinox.internal.provisional.spi.p2.artifact.repository.SimpleArtifactRepositoryFactory;
import org.eclipse.equinox.internal.provisional.spi.p2.metadata.repository.SimpleMetadataRepositoryFactory;
import org.eclipse.equinox.p2.publisher.IPublisherAction;
import org.eclipse.equinox.p2.publisher.IPublisherInfo;
import org.eclipse.equinox.p2.publisher.Publisher;
import org.eclipse.equinox.p2.publisher.PublisherInfo;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAction;
import org.eclipse.equinox.internal.p2.repository.helpers.AbstractRepositoryManager;
public class PublisherExample implements IApplication {
public Object start(IApplicationContext context) throws Exception {
IPublisherInfo info = createPublisherInfo();
IPublisherAction[] actions = createActions();
Publisher publisher = new Publisher(info);
publisher.publish(actions, new NullProgressMonitor());
return null;
}
public void stop() {
}
public static 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("path to create the repo"),
"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("path to create the repo"),
"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 static IPublisherAction[] createActions() {
IPublisherAction[] result = new IPublisherAction[2];
File[] bundleLocations = new File[1];
bundleLocations[0] = new File("location of the feature folder");
BundlesAction bundlesAction = new BundlesAction(bundleLocations);
FeaturesAction featureAction = new FeaturesAction(bundleLocations);
result[0] = bundlesAction;
result[1] = featureAction;
return result;
}
}
In here, you have to define the
path you want to create the P2 repo which is called as
metadataRepository. At the same time, you have to specify the source path where it contains bundles and features that you want to added to the P2 Repo.