You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Spring Boot应用传递--add-module参数?

Spring Boot 通过 pom.xml 配置编译与运行时的 --add-module 参数

1. 编译时参数配置

使用 maven-compiler-plugin 配置编译阶段的 JVM 参数,确保编译过程中加载指定模块:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version> <!-- 使用与项目兼容的稳定版 -->
      <configuration>
        <compilerArgs>
          <!-- 替换为依赖要求的模块名,多模块用逗号分隔 -->
          <arg>--add-module=你的目标模块名</arg>
        </compilerArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

2. Maven 直接运行时的参数配置

通过 spring-boot-maven-plugin 配置 jvmArguments,确保执行 mvn spring-boot:run 时自动传递参数:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>3.2.0</version> <!-- 匹配你的Spring Boot版本 -->
  <configuration>
    <jvmArguments>--add-module=你的目标模块名</jvmArguments>
  </configuration>
</plugin>

3. 打包后可执行 Jar 的参数配置

如果需要让打包后的 Jar 运行时自动加载模块,可开启 executable 配置,将参数嵌入启动脚本:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>3.2.0</version>
  <configuration>
    <jvmArguments>--add-module=你的目标模块名</jvmArguments>
    <executable>true</executable> <!-- 生成可执行Jar,参数会嵌入启动逻辑 -->
  </configuration>
</plugin>

完整整合示例

将上述配置合并到 pom.xml 的 <build> 节点,一次性覆盖编译、Maven运行、打包后运行三个场景:

<build>
  <plugins>
    <!-- 编译阶段参数 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
      <configuration>
        <compilerArgs>
          <arg>--add-module=你的目标模块名</arg>
        </compilerArgs>
      </configuration>
    </plugin>

    <!-- Spring Boot运行与打包参数 -->
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>3.2.0</version>
      <configuration>
        <jvmArguments>--add-module=你的目标模块名</jvmArguments>
        <executable>true</executable>
      </configuration>
    </plugin>
  </plugins>
</build>

注意事项

  • 替换 你的目标模块名 为依赖要求的具体模块,多模块可使用逗号分隔(如 --add-module=module1,module2)
  • 确保插件版本与你的 Spring Boot 版本兼容:Spring Boot 3.x 要求 maven-compiler-plugin 3.8+,Java 版本至少 17
  • 若项目本身是模块化项目(含 module-info.java),可优先通过 requires 声明依赖模块,部分场景仍需配合 --add-module 使用

内容的提问来源于stack exchange,提问作者tuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:35:16