项目和BOM依赖有什么区别?

问题描述 投票:0回答:1

我注意到 fabric8.io for Kubernetes 客户端有两个以 project 和 BOM 结尾的依赖关系。

我注意到的唯一区别是,它首先有一个分布式版本。另外根据apache指南,bom通常作为project的父类使用。

还有其他用途上的区别吗?我应该使用Spring Boot的哪个依赖关系?

java spring-boot maven gradle repository
1个回答
2
投票

BOM项目可以作为你的Maven模块的父项目,也可以作为BOM依赖项导入,这样你就可以从该BOM导入依赖项。关于这个问题,可以找到一篇非常好的文章。此处.

为什么BOM很重要?既然你在你的问题中添加了一个Spring标签,那么假设你想使用某个Spring版本,而component_1和component_2只要有相同的版本就能正常工作。作为一个库开发者,你会有一个版本的BOM,其中包含了component_1和component_2,在你的项目中,你需要导入你所需要的版本的BOM和没有版本的组件,因为它将从你导入的BOMparent继承。这正是Spring的做法。

为了防止上面的链接将来不能用,这里是BOM的基本工作流程。

// BOM project
<project ...>

    <modelVersion>4.0.0</modelVersion>
    <groupId>baeldung</groupId>
    <artifactId>Baeldung-BOM</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>BaelDung-BOM</name>
    <description>parent pom</description>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>test</groupId>
                <artifactId>a</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>test</groupId>
                <artifactId>b</artifactId>
                <version>1.0</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>test</groupId>
                <artifactId>c</artifactId>
                <version>1.0</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
// importing the BOM in your project
<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>baeldung</groupId>
    <artifactId>Test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Test</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>baeldung</groupId>
                <artifactId>Baeldung-BOM</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <dependency>
                <groupId>test</groupId>
                <artifactId>b</artifactId>
                <!-- version and scope omitted, inherited from the BOM, 1.0 and compile (you can override them here, but that defeats the purpose) -->
            </dependency>
    </dependencies>
</project>

请注意,导入一个BOM并不会添加它的所有依赖关系。dependencyManagement 部分,除非您在 dependencies 部分的项目。它就像一个产品目录,它向你展示了BOM提供给你的东西。

这里是 是Spring Boot 2.3.0的依赖关系pom.xml,其中有 dependencyManagement 部分,看看现实世界中的BOM是怎样的(如果你想的话,也可以只看父级)。

如果你曾经想使用Spring 6、Hibernate 5和JUnit 5 & Assertion lib朋友,假设它们都提供了BOM,你可以包含这3个BOM,每次你需要为你的项目升级Spring版本时,你需要的只是更新导入的Spring BOM的版本。

© www.soinside.com 2019 - 2024. All rights reserved.