本文介绍如何改造 spring boot 服务的 maven 构建流程,将插件从“打包进 uber jar”改为“按插件目录结构分离部署”,配合 docker 实现运行时独立类加载。
本文介绍如何改造 spring boot 服务的 maven 构建流程,将插件从“打包进 uber jar”改为“按插件目录结构分离部署”,配合 docker 实现运行时独立类加载。
要实现插件的运行时独立类加载(例如通过自定义 URLClassLoader 加载各插件 JAR 及其专属依赖),关键在于解耦构建时的依赖聚合与部署时的目录隔离。核心思路是:移除插件作为 server 模块的 compile/runtime 依赖,转而通过 Maven Assembly Plugin 显式拉取并组织插件及其完整依赖树到独立目录中。
✅ 步骤一:清理 server 的 pom.xml
将原
<!-- ❌ 移除以下类型依赖 --> <dependency><groupid>com.example</groupid><artifactid>plugin-a</artifactid><version>1.0.0</version></dependency>
✅ 步骤二:创建插件装配模块(推荐新建 plugins-assembly 子模块)
在该模块的 pom.xml 中配置 maven-assembly-plugin,并定义 assembly.xml:
<plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-assembly-plugin</artifactid><version>3.6.0</version><configuration><descriptors><descriptor>src/assembly/assembly.xml</descriptor></descriptors><outputdirectory>${project.build.directory}/plugins</outputdirectory></configuration><executions><execution><id>make-plugins-bundle</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin>
src/assembly/assembly.xml 示例(按插件分目录,每个插件含自身 JAR + 所有 runtime 依赖):
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/ASSEMBLY/2.2.0 https://maven.apache.org/xsd/assembly-2.2.0.xsd"><id>plugins</id><formats><format>dir</format></formats><includebasedirectory>false</includebasedirectory><!-- plugin-a 及其依赖 --><dependencysets><dependencyset><outputdirectory>plugin-a</outputdirectory><includes><include>com.example:plugin-a:*:jar</include></includes><useprojectartifact>false</useprojectartifact><scope>runtime</scope><unpack>false</unpack></dependencyset><dependencyset><outputdirectory>plugin-a/lib</outputdirectory><includes><include>com.example:plugin-a:*:jar</include></includes><excludes><exclude>com.example:plugin-a:*:jar</exclude></excludes><useprojectartifact>false</useprojectartifact><scope>runtime</scope><unpack>false</unpack></dependencyset></dependencysets><!-- 同理为 plugin-b, plugin-c... 添加独立 dependencySet --></assembly>
⚠️ 注意:Maven 默认不会自动解析某个 artifact 的 全部 runtime 依赖 并按需归集。因此更稳健的做法是——为每个插件单独声明一个
,并通过 ;或改用 maven-dependency-plugin 的 copy-dependencies 目标分步拉取。精确指定该插件及其显式依赖(可配合 mvn dependency:tree -Dincludes=... 辅助分析)
✅ 步骤三:Docker 构建集成
在 Dockerfile 中,将 assembly 生成的 plugins/ 目录复制到镜像指定路径(如 /app/plugins),并与 server JAR 分离部署:
FROM openjdk:17-jre-slim # 复制主服务 JAR(来自 server 模块) COPY target/server.jar /app/server.jar # 复制插件目录(来自 plugins-assembly 模块) COPY plugins-assembly/target/plugins /app/plugins ENTRYPOINT ["java", "-jar", "/app/server.jar"]
此时容器内结构为:
/app/
├── server.jar # 无插件代码,轻量纯净
└── plugins/
├── plugin-a/
│ ├── plugin-a-1.0.0.jar
│ └── lib/
│ ├── guava-32.0.0-jre.jar
│ └── ...
└── plugin-b/
├── plugin-b-1.0.0.jar
└── lib/
└── ...
✅ 运行时加载建议
在 server 启动后,遍历 /app/plugins/**/plugin-*.jar,为每个插件创建独立 URLClassLoader,传入对应 lib/ 下的依赖 JAR 路径,即可实现真正的类加载隔离与热插拔基础。
总结:该方案通过 Maven 构建职责分离(server ≠ plugins)、Assembly 精确控制输出结构、Docker 静态目录映射,兼顾构建可重现性与运行时灵活性,是微内核架构下插件化服务的推荐实践。











