006 Maven 多模块 + Java 工程读法

用 ai-native-service / auto-layout-service 的 common、client、web 模块,读懂工程边界、依赖来源和 Java 8/21 混合模块

1. 先看根 pom,不先钻代码

测试转开发读后端工程时,第一步不是找某个类,而是打开根目录的 pom.xml。它告诉你三件事:这是一个父工程、下面有哪些子模块、版本大多从哪里来。

读法

<packaging>pom</packaging> 通常表示“聚合父工程”,不是直接启动的业务包。真正启动服务的,通常在 *-web 模块里。

ai-native-service / auto-layout-service
├── pom.xml                         父工程:声明 modules、父依赖、统一版本
├── *-common / pom.xml              共享 DTO / VO / 枚举 / 契约模型
├── *-client / pom.xml              给别的服务调用自己的客户端包
└── *-web / pom.xml                 Controller / Service / Manager / Mapper,启动入口

两个仓库都是真实的三模块结构:

仓库父 artifactIdmodules启动模块
ai-native-serviceai-native-service-parentai-native-service-common
ai-native-service-client
ai-native-service-web
ai-native-service-web
auto-layout-serviceauto-layout-service-parentauto-layout-service-common
auto-layout-service-client
auto-layout-service-web
auto-layout-service-web

2. common、client、web 分别管什么

你可以先用“谁会依赖谁”来判断边界:

common:共享数据结构 → client:远程调用封装 → web:真正业务服务
模块主要内容AIHom 读代码时怎么用
common请求 / 响应 VO、DTO、枚举、校验注解等共享对象看到接口入参或返回值,先到这里找字段含义
client给其他服务使用的调用封装,例如 AiNativeClient跨服务调用时,先看 client 暴露了哪些方法
webController、Service、Factory、Manager、Mapper、配置和启动类新增功能大多数改这里;排查问题也从这里追调用链
真实例子

auto-layout-service-web 依赖了 ai-native-service-client,代码里 AiNativeManager 注入 AiNativeClient,再调用 imageGenerateWithPolling。这说明 client 是“跨服务调用的门面”。

3. 依赖版本从哪里来

Maven 查版本时,不要只看当前模块。顺序通常是:

  1. 当前模块 <dependency> 自己写了 <version>,就用它。
  2. 当前模块没写版本,去父工程的 <dependencyManagement> 找。
  3. 父工程也可能继承公司统一 BOM,比如 pilot-boot-dependencies。
  4. 某些 BOM 用 type=pom + scope=import 导入一整套版本。
<dependencyManagement>
  <dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bom</artifactId>
    <version>${spring-ai.version}</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencyManagement>

ai-native-service 的父 POM 里能看到 spring-ai.version、spring-ai-alibaba.version、netty.version、mybatis-plus.version 这些集中版本。ai-native-service-web 里再使用 spring-ai-openai、spring-ai-starter-mcp-client 等依赖时,就不一定需要逐个写版本。

小心

课程里不要把“没写 version”理解成“没有版本”。很多版本被父 POM 或 BOM 管住了。测试定位依赖冲突时,要顺着父子 POM 往上找。

4. Java 8 / 21 混合模块怎么读

ai-native-service 父工程声明 java.version=21,但 ai-native-service-common 和 ai-native-service-client 都用 maven-compiler-plugin 的 <release>8</release>。这不是矛盾,而是边界设计。

位置Java 版本信号意思
ai-native-service/pom.xmljava.version=21web 服务主工程可以使用 Java 21 / Spring Boot 3 生态
ai-native-service-common/pom.xmlrelease 8共享模型尽量兼容老服务
ai-native-service-client/pom.xmlrelease 8客户端包要给 Java 8 服务依赖,比如 auto-layout-service
auto-layout-service/pom.xmljava.version=1.8自动布局服务整体还是 Java 8 风格

所以读代码时要形成一个直觉:common / client 是契约边界,通常更保守;web 是业务运行边界,版本可能更新。

5. 一分钟读工程步骤

  1. 打开根 pom.xml,确认 packaging、modules、父依赖。
  2. 打开 *-web/pom.xml,找 spring-boot-maven-plugin 的 mainClass。
  3. 看 *-web 依赖了哪些 common、client 和外部 SDK。
  4. 遇到请求 / 响应类,先按名字去 common 或 web/.../vo 找字段。
  5. 遇到版本疑问,先查当前 POM,再查父 POM,再查 BOM。

6. 代码证据

你要确认什么真实文件
两个服务都有 common/client/web 三模块ai-native-service/pom.xml
auto-layout-service/pom.xml
ai-native-service 父工程 Java 21、Spring AI BOMai-native-service/pom.xml
ai-native-service-common / client 编译 Java 8ai-native-service/ai-native-service-common/pom.xml
ai-native-service/ai-native-service-client/pom.xml
auto-layout-service Java 8,web 依赖 ai-native-service-clientauto-layout-service/pom.xml
auto-layout-service/auto-layout-service-web/pom.xml
跨服务 client 真实调用AiNativeClient
AiNativeManager

7. 自检 Quiz

Q1: 根 POM 里的 <packaging>pom</packaging> 最像在说什么?
Q2: 其他服务想调用 ai-native-service,优先依赖哪个模块?
Q3: 依赖没写 version,最可能先去哪里找版本?
卡住时可以直接问 agent:“帮我从某个 POM 追一个依赖版本从哪里来”,或者“帮我判断这个类应该放 common、client 还是 web”。
推荐主资料

想补 Maven 基础,优先看 Apache Maven 官方的 Introduction to the POM。读完这节课后,只需要重点理解 parent、modules、dependencyManagement、properties。