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

如何为Maven多模块Java项目创建BOM文件

为Maven多模块类库创建BOM文件的方法

要给你的多模块类库生成BOM,根本不需要额外插件——BOM本身就是个特殊的Maven项目,直接新增一个模块就能搞定,步骤如下:

1. 新增BOM模块

在你的多模块项目根目录下,创建一个新的模块(比如命名为my-library-bom),模块的pom.xml配置如下:

<?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>
        <!-- 指向你的项目父pom -->
        <groupId>com.yourcompany</groupId>
        <artifactId>my-library-parent</artifactId>
        <version>${project.version}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>my-library-bom</artifactId>
    <!-- BOM的打包类型必须是pom -->
    <packaging>pom</packaging>
    <name>My Library BOM</name>

    <dependencyManagement>
        <dependencies>
            <!-- 列出你所有的5个模块依赖,版本用项目统一版本号 -->
            <dependency>
                <groupId>com.yourcompany</groupId>
                <artifactId>module-1</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.yourcompany</groupId>
                <artifactId>module-2</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!-- 依次添加剩下的3个模块 -->
            <dependency>
                <groupId>com.yourcompany</groupId>
                <artifactId>module-3</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.yourcompany</groupId>
                <artifactId>module-4</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.yourcompany</groupId>
                <artifactId>module-5</artifactId>
                <version>${project.version}</version>
            </dependency>

            <!-- 可选:如果你的类库依赖了第三方包,也可以把它们的版本统一管理在这里 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.14.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2. 将BOM模块加入父pom的模块列表

打开项目根目录的父pom.xml,在<modules>节点里新增这个BOM模块:

<modules>
    <module>module-1</module>
    <module>module-2</module>
    <module>module-3</module>
    <module>module-4</module>
    <module>module-5</module>
    <!-- 新增BOM模块 -->
    <module>my-library-bom</module>
</modules>

3. 构建并发布

执行常规的Maven构建命令:

mvn clean install

如果要发布到远程仓库,执行:

mvn clean deploy

这样BOM会和其他模块一起被构建、安装到本地仓库或发布到远程仓库,版本号和其他模块完全一致。

4. 其他项目如何使用这个BOM

其他应用只需要在自己的pom.xml的<dependencyManagement>里导入这个BOM,之后引入你的类库模块时就不需要指定版本号了:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.yourcompany</groupId>
            <artifactId>my-library-bom</artifactId>
            <version>1.0.0</version> <!-- 这里只需要指定BOM的版本 -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 直接引入模块,无需版本号 -->
<dependencies>
    <dependency>
        <groupId>com.yourcompany</groupId>
        <artifactId>module-1</artifactId>
    </dependency>
    <dependency>
        <groupId>com.yourcompany</groupId>
        <artifactId>module-3</artifactId>
    </dependency>
</dependencies>

注意事项

  • 确保所有模块的版本号都通过父pom的${project.version}统一管理,这样BOM里的版本会自动和其他模块保持一致。
  • 如果后续模块版本升级,只需要修改父pom的版本号,重新构建发布即可,使用方只需要更新BOM的版本号,所有依赖的模块版本都会同步升级。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 14:12:26