PF4J Java Plugin Framework

repository·master·Indexed 25 days ago

https://github.com/pf4j/pf4j

A lightweight, open-source Java plugin framework for transforming monolithic applications into modular ones. It enables third-party extensions via ExtensionPoints and provides a PluginManager for loading, starting, and interacting with plugins using JAR or ZIP formats.

Tokens
1.3K
Snippets
6
Records
6
Agent score
33%

What's inside PF4J

  1. Build the PF4J Gradle Demo

    master

    To build the PF4J Gradle demo project, navigate to the demo_gradle directory and use the Gradle wrapper to run the build command. This process generates the application uberjar and the plugin zip files.

    Build Outputs:

    • Application: app/build/libs/app-plugin-demo-uberjar.jar
    • Plugins: Three zip files located in build/plugins/:
      • plugin-hello-plugin-0.0.1.zip
      • plugin-KotlinPlugin-1.0.0.zip
      • plugin-welcome-plugin-0.0.1.zip
    cd demo_gradle
    ./gradlew build
  2. Manage Plugins using PluginManager

    master

    Use a PluginManager implementation to load, start, and interact with plugins. Common implementations include JarPluginManager, ZipPluginManager, and DefaultPluginManager (which combines both).

    public static void main(String[] args) {
        // create the plugin manager
        PluginManager pluginManager = new JarPluginManager();
        
        // start and load all plugins of application
        pluginManager.loadPlugins();
        pluginManager.startPlugins();
    
        // retrieve all extensions for "Greeting" extension point
        List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
        for (Greeting greeting : greetings) {
            System.out.println(">>> " + greeting.getGreeting());
        }
        
        // stop and unload all plugins
        pluginManager.stopPlugins();
        pluginManager.unloadPlugins();
    }
  3. Run the PF4J Gradle Demo

    master

    Run the demo application using the Gradle task app:run. The application will initialize the PF4J plugin manager, expand the plugin zip files found in the plugins directory, resolve the plugins, and start them. The demo demonstrates extension point discovery (e.g., finding extensions for org.pf4j.demo.api.Greeting) and the plugin lifecycle (start/stop).

    ./gradlew app:run
  4. Define Extension Points and Extensions

    master

    To create a modular system, define an interface or abstract class that implements the ExtensionPoint marker interface. This serves as the point where custom code can be injected. To provide an implementation, create a class and annotate it with @Extension.

    // 1. Define the extension point
    public interface Greeting extends ExtensionPoint {
        String getGreeting();
    }
    
    // 2. Create an extension implementation
    @Extension
    public class WelcomeGreeting implements Greeting {
        public String getGreeting() {
            return "Welcome";
        }
    }
  5. Implement a Plugin with Lifecycle Methods

    master

    If you need to manage lifecycle events such as start, stop, or delete, create a class that extends Plugin. This is optional; if you only need to provide extensions without lifecycle hooks, you do not need to supply a Plugin class.

    public class WelcomePlugin extends Plugin {
    
        @Override
        public void start() {
            System.out.println("WelcomePlugin.start()");
        }
    
        @Override
        public void stop() {
            System.out.println("WelcomePlugin.stop()");
        }
        
        @Override
        public void delete() {
            System.out.println("WelcomePlugin.delete()");
        }
        
    }
  6. Configure Plugin Metadata in MANIFEST.MF

    master

    When distributing a plugin as a JAR file, you must include metadata in the MANIFEST.MF file. Key attributes include:

    • Plugin-Id: The unique identifier for the plugin (Mandatory).
    • Plugin-Version: The version of the plugin (Mandatory).
    • Plugin-Class: The fully qualified name of your Plugin class (Optional).
    • Plugin-Dependencies: A comma-separated list of plugin IDs that this plugin depends on (Optional).
    • Plugin-Provider: The name of the plugin author (Optional).
    Manifest-Version: 1.0
    Archiver-Version: Plexus Archiver
    Created-By: Apache Maven
    Built-By: decebal
    Build-Jdk: 1.6.0_17
    Plugin-Class: org.pf4j.demo.welcome.WelcomePlugin
    Plugin-Dependencies: x, y, z
    Plugin-Id: welcome-plugin
    Plugin-Provider: Decebal Suiu
    Plugin-Version: 0.0.1