如何在同一 reactor 中先构建 React 再打包到 Spring Boot JAR?

解读

面试官真正想考察的是“Maven 多模块 reactor 的执行顺序控制能力”与“前端资源如何无缝嵌入 Spring Boot 最终可执行 JAR”。
国内主流场景是:

  1. 前端团队用 React + Vite/Webpack 产出静态资源;
  2. 后端团队用 Spring Boot 提供 API 并把静态页做前后端一体化部署;
  3. 一条 mvn clean package 命令在 CI(如 GitLab-CI、阿里云云效、腾讯云 CODING)里跑通,不允许手动拷贝、不允许两次 git clone。
    因此,必须让 Maven 在 reactor 内部先完成 npm 构建,再把产物塞进 Spring Boot 的 classpath:/static/,最终打出一个可运行的 fat-jar。

知识点

  1. reactor 排序规则:Maven 按 模块间依赖声明 <dependency> 与 <parent> 关系计算有向无环图,无依赖的模块先执行。
  2. frontend-maven-plugin:官方插件,可在 reactor 内部自动下载 Node.js 与 npm,执行 npm ci && npm run build,产出 dist/ 目录。
  3. maven-resources-plugin 或 maven-antrun-plugin:把 dist/ 目录复制到 ../boot-module/src/main/resources/static/,保证 Spring Boot 的默认静态资源路径生效。
  4. spring-boot-maven-plugin 的 repackage 目标:在 package 生命周期 末端把 BOOT-INF/classes/static/ 打进 fat-jar。
  5. <optional>true</optional> 或 <scope>provided</scope>:避免 React 模块被意外依赖,导致循环依赖或打包膨胀。
  6. CI 缓存:国内网络下,frontend-maven-plugin 支持 <npmDownloadRoot> 指向阿里云镜像,<nodeDownloadRoot> 指向淘宝 Node 镜像,加速构建。
  7. profile 隔离:使用 -Pfrontend 只在 CI 环境触发 npm 构建,本地开发仍走 npm run dev 热更新,提升体验。

答案

  1. 创建 Maven 多模块工程
    ├── pom.xml(父 POM,<packaging>pom</packaging>)
    ├── react-ui(<packaging>jar</packaging>,仅用于构建前端)
    └── boot-app(<packaging>jar</packaging>,依赖 spring-boot-starter-web)

  2. 在 react-ui/pom.xml 中声明无依赖项,确保 reactor 先构建它:
    <build>
    <plugins>
    <plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.15.0</version>
    <configuration>
    <nodeVersion>v18.17.1</nodeVersion>
    <npmVersion>9.6.7</npmVersion>
    <installDirectory>project.basedir</installDirectory></configuration><executions><execution><id>installnodeandnpm</id><goals><goal>install−node−and−npm</goal></goals></execution><execution><id>npmci</id><goals><goal>npm</goal></goals><configuration><arguments>ci−−registry=https://registry.npmmirror.com</arguments></configuration></execution><execution><id>npmrunbuild</id><goals><goal>npm</goal></goals><configuration><arguments>runbuild</arguments></configuration></execution></executions></plugin><!−−把dist目录attach成zip,供后续模块解包−−><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build−helper−maven−plugin</artifactId><version>3.4.0</version><executions><execution><id>attach−artifact</id><phase>package</phase><goals><goal>attach−artifact</goal></goals><configuration><artifacts><artifact><file>{project.basedir}</installDirectory> </configuration> <executions> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> </execution> <execution> <id>npm ci</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>ci --registry=https://registry.npmmirror.com</arguments> </configuration> </execution> <execution> <id>npm run build</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>run build</arguments> </configuration> </execution> </executions> </plugin> <!-- 把 dist 目录 attach 成 zip,供后续模块解包 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.4.0</version> <executions> <execution> <id>attach-artifact</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>{project.build.directory}/../dist</file>
    <type>zip</type>
    <classifier>static</classifier>
    </artifact>
    </artifacts>
    </configuration>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

  3. 在 boot-app/pom.xml 中依赖 react-ui 的 zip 构件,并解压到 target/classes/static/:
    <dependencies>
    <dependency>
    <groupId>com.yourcompany</groupId>
    <artifactId>react-ui</artifactId>
    <version>project.version</version><type>zip</type><classifier>static</classifier><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven−dependency−plugin</artifactId><version>3.6.0</version><executions><execution><id>unpack−static</id><phase>generate−resources</phase><goals><goal>unpack−dependencies</goal></goals><configuration><includeArtifactIds>react−ui</includeArtifactIds><includeClassifiers>static</includeClassifiers><outputDirectory>{project.version}</version> <type>zip</type> <classifier>static</classifier> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>unpack-static</id> <phase>generate-resources</phase> <goals> <goal>unpack-dependencies</goal> </goals> <configuration> <includeArtifactIds>react-ui</includeArtifactIds> <includeClassifiers>static</includeClassifiers> <outputDirectory>{project.build.outputDirectory}/static</outputDirectory>
    </configuration>
    </execution>
    </executions>
    </plugin>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>3.2.5</version>
    <executions>
    <execution>
    <goals>
    <goal>repackage</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

  4. 在父 POM 中强制 reactor 顺序:
    <modules>
    <module>react-ui</module>
    <module>boot-app</module>
    </modules>

  5. 运行 mvn clean package -Pfrontend(frontend profile 绑定 react-ui 插件),即可在 boot-app/target/boot-app-1.0.0.jar 内看到 BOOT-INF/classes/static/ 包含 React 构建产物,java -jar 直接启动即可访问首页。

拓展思考

  1. 若前端项目巨大,npm run build 耗时 5 分钟以上,可拆成独立 GitLab CI Job 并上传静态包到 Nexus/阿里云 OSS,后端通过 maven-download-plugin 拉取,减少 reactor 时间。
  2. 对 微服务多 UI 场景,可抽象出 common-static 模块,用 <classifier> 区分主题包,实现同一后端多套皮肤。
  3. 若需 Server-Side Rendering (SSR),可引入 spring-boot-starter-thymeleaf 与 react-dom/server,在 JVM 内执行 JavaScript(GraalVM JS 引擎),此时需要 frontend-maven-plugin 额外产出 server-bundle.js,并通过 Nashorn/GraalJS 脚本引擎渲染,**Maven 需要把 server-bundle.js 打进 JAR 的 BOOT-INF/lib/ 并设置 ResourceLoader 路径。
  4. 国内信创环境(鲲鹏、麒麟)下,Node.js 官方无 ARM 二进制,需提前在 Nexus 私服上传 ARM 版本,frontend-maven-plugin 通过 <nodeDownloadRoot> 指向私服,避免构建失败。