简体   繁体   中英

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.

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.

Are there any other uses/differences? Which dependency should I use with Spring Boot?

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. A really good article on this matter can be found here .

Why is a BOM important? 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. 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. This is exactly what Spring does.

In case the link above won't work in the future, here is the basic workflow with BOMs.

// 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. It's like a product catalog, it shows you what the BOM is offering you.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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