Tuesday, April 23, 2013

Generating p2-repo for custom WSO2 Features, and Components



Here, I'm going to discuss how to generate a p2-repo for custom, user-written WSO2 components, and features. This can be easily achieved using the maven plugin, carbon-p2-plugin, that WSO2 have written.
First, you may have a look at Creating-your-own-wso2-carbon-components webinar to know how to write carbon components, and a feature for those components. It covers quite a lot of basics, and best practices as well.
Now, to install these features to WSO2 products like WSO2 Enterprise Service Bus, you need to generate a p2-repository. Then, you can add that p2-repo via the "Feature Management" utility in management console, and install the features you want. p2-repo concept comes from the underline Eclipse equinox project that WSO2 products use. In the following I will tell you how to generate a p2-repo for your features.
  • Simply create a new maven project (packaging: pom)
  • Add carbon-p2-plugin as a maven plugin
    <plugin>
        <groupId>org.wso2.maven</groupId>
        <artifactId>carbon-p2-plugin</artifactId>
        <version>1.5.2</version>
        ---
    </plugin>
  • Then set the features you want to publish under the carbon-p2-plugin plugin configuration as shown in the following sample pom.xml.  
  • This sample pom.xml was copied from the p2-repo generation pom.xml of carbon 4.1.0, and I simplified it for clarity.
  • There are two sample feature definitions I have specified. Replace those featureArtifactDef with your own feature definitions. The format is $groupId:$artifactId:$version.

I have tested this pom file, and it worked for me just fine. 
When you build this via maven, maven creates the target/p2-repo directory. This has the p2-repository which contains the complete p2-repo including artifacts.jar and content.jar. You can just use this folder to install features, or you can host it somewhere. There're no special requirements on hosting.


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <parent>
        <groupId>org.wso2.carbon</groupId>
        <artifactId>carbon-features</artifactId>
        <version>4.1.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>mysample-feature-repository</artifactId>
    <version>4.1.0</version>
    <packaging>pom</packaging>
    <name>WSO2 Carbon - Feature Repository</name>

    <build>
      <plugins>
        <plugin>
          <groupId>org.wso2.maven</groupId>
            <artifactId>carbon-p2-plugin</artifactId>
            <version>1.5.2</version>
            <executions>
              <execution>
                <id>2-p2-repo-generation</id>
                <phase>package</phase>
                <goals>
                  <goal>p2-repo-gen</goal>
                 </goals>
                 <configuration>
                   <p2AgentLocation>${basedir}/target/p2-agent</p2AgentLocation>
                   <metadataRepository>file:${basedir}/target/p2-repo</metadataRepository>
                   <artifactRepository>file:${basedir}/target/p2-repo</artifactRepository>
                   <publishArtifacts>true</publishArtifacts>
                   <publishArtifactRepository>true</publishArtifactRepository>
                   <featureArtifacts> 

<!-- change the featureArtifactDef to match your needs -->
                     <featureArtifactDef>
                        org.wso2.carbon:org.wso2.carbon.service.mgt.feature:4.1.0
                     </featureArtifactDef>
                     <featureArtifactDef>
                       org.wso2.carbon:org.wso2.carbon.registry.core.feature:4.1.0
                     </featureArtifactDef>


               </featureArtifacts>
             </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>