How to Package the Ark Plugin

Updated at:

This sample project demonstrates how to use the Maven plug-in to package a common Java project into a standard Ark plug-in.

Background information

In actual development, dependency package conflicts are often encountered. Assuming that a class library sample-lib is developed, a business application may conflict with existing dependencies when it is introduced and used. In this case, developers need to isolate their own class libraries from other dependencies of the business, and do not negotiate with each other to dependency package the version. Ark Plugin is a product of practice based on this demand background.

Ark Plugin runs on Ark Container. The container is responsible for loading and starting. Any Ark Plugin is loaded by an independent ClassLoader, which is isolated from each other. The Ark Plugin has four concepts:

  • Import class: When the plug-in is started, it is preferentially entrusted to the plug-in that exports the class. If it cannot be loaded, it will try to load from the plug-in.

  • Export class: If other plug-ins import this class, they are loaded first from this plug-in.

  • Import resources: When a plug-in searches for resources, it preferentially delegates the load to the plug-in that exports the resources. If the load fails, it attempts to load the resources from the plug-in.

  • Export resources: If other plug-ins import the resources, they are preferentially loaded from this plug-in.

For more information about plug-in specifications, see Plug-in specifications.

Use tools

SOFAArk provides the Maven plug-in sofa-ark-plugin-maven-plugin by default. You can package common Java projects into a standard Ark plug-in by simple configuration. The coordinates of the plug-in are as follows:

<plugin>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofa-ark-plugin-maven-plugin</artifactId>
    <version>${sofa.ark.version}</version>
</plugin>

For more information about plug-in configurations, see Complete configuration templates.

Procedure

  1. Create a standard Maven project.

    The use case project is a standard Maven project that contains two modules:

    • common module: contains the plug-in export class.

    • Plugin module: contains com.alipay.sofa.ark.spi.service.PluginActivator interface implementation class and a plug-in service class. The plug-in packaging tool sofa-ark-plugin-maven-plugin is configured in the pom.xml of this module.

  2. Configure the packaging plug-in.

    Configure the packaging plug-in in the pom.xml of the plugin module. Example:

    <build>
        <plugins>
            <plugin>
                <groupId>com.alipay.sofa</groupId>
                <artifactId>sofa-ark-plugin-maven-plugin</artifactId>
                <version>${project.version}</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>ark-plugin</goal>
                        </goals>
    
                        <configuration>
    
                        <!--can only configure no more than one activator-->
                        <activator>com.alipay.sofa.ark.sample.activator.SamplePluginActivator</activator>
    
                        <!-- configure exported class -->
                        <exported>
                            <!-- configure package-level exported class-->
                            <packages>
                                <package>com.alipay.sofa.ark.sample.common</package>
                            </packages>
    
                             <!-- configure class-level exported class -->
                            <classes>
                                <class>com.alipay.sofa.ark.sample.facade.SamplePluginService</class>
                            </classes>
                        </exported>
    
                        <!--specify destination where ark-plugin will be saved, default saved to ${project.build.directory}-->
                        <outputDirectory>../target</outputDirectory>
    
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    In the sample project, only a part of the configuration items is configured, but it is enough to generate a usable Ark plug-in. The meaning of each configuration items is as follows:

    • activator: the entry class of the Ark container startup plug-in. You can configure a maximum of one entry class.

      In the activator of the plug-in, some initialization operations are usually performed, such as publishing the plug-in service. In this sample project, the plug-in service is released.

    • Export package: export class configuration at the package level. All classes in the plug-in that are prefixed with the exported package name, including the third-party dependency package of the plug-in, are exported.

    • Export Class: the export class configuration of the exact class name to export the specific class.

    • outputDirectory: The directory in which the output Ark Plugin file is stored after packaging by using the mvn package command.

    In the sample project, only the classes created by the project are exported. In actual use, the three-party package on which the project depends can also be exported.

  3. Package, install, publish, and introduce the Ark Plugin.

    Similar to common engineering operations, you can use mvn package, mvn install, and mvn deploy to complete the installation and release of plug-in packages.

    Important

    The default published Ark Plugin whose Maven coordinates are incremented by classifier=ark-plugin. For example, if you want to use the Ark plug-in in the sample project, you must configure the dependencies as follows:

    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>sample-ark-plugin</artifactId>
        <classifier>ark-plugin</classifier>
        <version>${sofa.ark.version}</version>
    </dependency>
  4. Publish and reference a plug-in service.

    The following sample project demonstrates how to use PluginContext to publish a plug-in service:

    public class SamplePluginActivator implements PluginActivator{
    
        public void start(PluginContext context) throws ArkRuntimeException{
            System.out.println("starting in sample ark plugin activator");
            context.publishService(SamplePluginService.class,new SamplePluginServiceImpl());
        }
    
        public void stop(PluginContext context)throws ArkRuntimeException{
            System.out.println("stopping in ark plugin activator");
        }
    }

    At the same time, the service implementation SamplePluginServiceImpl demonstrates how to reference services published by other plug-ins or Ark containers. The event management service EventAdminService released by Ark containers is quoted here:

    public class SamplePluginServiceImpl implements SamplePluginService {
    
        @ArkInject
        private EventAdminService eventAdminService;
    
        public String service() {
            return "I'm a sample plugin service published by ark-plugin";
        }
    
        public void sendEvent(ArkEvent arkEvent) {
            eventAdminService.sendEvent(arkEvent);
        }
    }