pf4j 默认插件打包模式

pf4j 进行插件打包方法不少,比如fat jar 的,zip 文件的,以及文件夹的

扩展支持的模式

  • jar 模式(官方推荐的模式,对于依赖可以基于shared插件解决,注意对于契约服务scope 使用provide模式,不然插件运行会有问题,目前版本测试)
    参考配置(shade 插件)
 
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • 使用zip 文件
    zip 文件的约定,包含了classes以及lib 目录,classes是编译之后的class 文件,lib 是三方依赖
    参考maven 构建(可以基于maven 的assembly 插件),注意此中模式需要一个plugin.properties 文件
    主要提供元数据信息,当然也可以直接在classes 目录提供 MANIFEST.MF 但是不推荐,容易造成
    问题
 
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>pf4j-learning</artifactId>
        <groupId>com.dalong</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>loginplugind</artifactId>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <plugin.id>loginplugind</plugin.id>
        <plugin.class>com.com.dalong.MyLoginPluginD</plugin.class>
        <plugin.version>0.0.1</plugin.version>
        <plugin.provider>dalong</plugin.provider>
        <plugin.dependencies />
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.dalong</groupId>
            <artifactId>service-contract</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.pf4j</groupId>
            <artifactId>pf4j</artifactId>
            <version>3.6.0</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.hashids</groupId>
            <artifactId>hashids</artifactId>
            <version>1.0.3</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Plugin-Id>${plugin.id}</Plugin-Id>
                            <Plugin-Class>${plugin.class}</Plugin-Class>
                            <Plugin-Version>${plugin.version}</Plugin-Version>
                            <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                            <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

assembly.xml

<assembly>
    <id>app</id>
    <formats>
        <format>dir</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*:jar:*</include>
            </includes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>classes</outputDirectory>
            <includes>
                <include>**</include>
            </includes>
            <excludes>
                <exclude>*-javadoc.jar</exclude>
                <exclude>*-sources.jar</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>plugin.properties</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

plugin.properties 参考

plugin.class=com.dalong.MyLoginPluginD
plugin.dependencies=
plugin.id=loginplugind
plugin.provider=dalong
plugin.version=0.0.1

说明

对于plugins 目录我们也可以配置开启以及禁用那些扩展,具体参考官方资料,主要是enabled.txt 以及disabled.txt文件
参考插件目录

 
plugins
├── loginpluginb-1.0-SNAPSHOT.jar
├── loginpluginc-1.0-SNAPSHOT.jar
└── loginplugind-1.0-SNAPSHOT.zip
 

对于zip 文件pf4j 为了处理方便先进行解压,然后在进行加载,源码可以参考github 链接

参考资料

https://pf4j.org/doc/packaging.html
https://pf4j.org/doc/disable-plugins.html
https://github.com/rongfengliang/pf4j-learning.git

posted on 2022-02-16 19:14  荣锋亮  阅读(380)  评论(0编辑  收藏  举报

导航