简体   繁体   English

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

[英]Whats the difference between project and BOM dependencies?

I've noticed that fabric8.io for Kubernetes client has two dependencies ending with project and BOM.我注意到 Kubernetes 客户端的 fabric8.io 有两个以项目和 BOM 结尾的依赖项。

The only difference I've noticed is that it first has a distributed version.我注意到的唯一区别是它首先有一个分布式版本。 Also according to apache guides, bom usually used as a parent for projects.另外根据 apache 指南,bom 通常用作项目的父级。

Are there any other uses/differences?还有其他用途/区别吗? Which dependency should I use with Spring Boot?我应该在 Spring 引导中使用哪个依赖项?

A BOM project can be either used as a parent for your Maven module or imported as a BOM dependency which allows you to import dependencies from that BOM. BOM 项目既可以用作 Maven 模块的父项,也可以作为 BOM 依赖项导入,允许您从该 BOM 导入依赖项。 A really good article on this matter can be found here .关于这个问题的一篇非常好的文章可以在这里找到。

Why is a BOM important?为什么 BOM 很重要? Since you've added a Spring tag to your question, let's say you want to use a certain Spring version and component_1 works fine with component_2 as long as they have the same version.由于您已在问题中添加了 Spring 标签,假设您想使用某个 Spring 版本,并且只要它们具有相同的版本,component_1 就可以与 component_2 一起正常工作。 As a library developer, you would have a versioned BOM which contains component_1 and component_2 and in your project, you would need to import the BOM with the version you need and the components you need without the version, as it will be inherited from your imported BOM/parent.作为库开发人员,您将拥有一个包含 component_1 和 component_2 的版本化 BOM,并且在您的项目中,您需要导入带有您需要的版本的 BOM 和您需要的没有版本的组件,因为它将继承自您导入的物料清单/父级。 This is exactly what Spring does.这正是 Spring 所做的。

In case the link above won't work in the future, here is the basic workflow with BOMs.如果上面的链接将来无法使用,这里是 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>

Note that importing a BOM does NOT add all of its dependencies specified in the dependencyManagement section, unless you add them in the dependencies section of your project.请注意,导入 BOM 不会添加在dependencyManagement部分中指定的所有依赖项,除非您将它们添加到项目的dependencies项部分中。 It's like a product catalog, it shows you what the BOM is offering you.它就像一个产品目录,它向您展示 BOM 为您提供的内容。

Here is the Spring Boot 2.3.0 dependencies pom.xml, with dependencyManagement section to see how a real world BOM looks like (or just parent, if you want). 是 Spring 启动 2.3.0 依赖项 pom.xml,带有dependencyManagement部分,以查看真实世界 BOM 的样子(或者只是父项,如果您愿意)。

If you ever wanted to use Spring 6, Hibernate 5 and JUnit 5 & Assertion lib friends, assuming that all of them provide a BOM, you could include those 3 BOMs and every time you need to upgrade Spring version for your project, all you'd need is an update to the imported Spring BOM's version. If you ever wanted to use Spring 6, Hibernate 5 and JUnit 5 & Assertion lib friends, assuming that all of them provide a BOM, you could include those 3 BOMs and every time you need to upgrade Spring version for your project, all you' d 需要更新导入的 Spring BOM 的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM