“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

What BOM?

BOM (Bill of Materials) is provided by Maven by defining a set of compatible JAR versions,

When using, you only need to rely on the BOM file, and you can safely use the required dependent JAR package, and you do not need to specify the version number.

The BOM maintainer is responsible for version upgrade and compatibility between JAR packages defined in the BOM.

why BOM?

In addition to making it easier for users to declare dependent clients without specifying a version number,

The main reason is to resolve dependency conflicts, such as considering the following dependency scenarios:

Project A relies on project B 2.1 and Project C 1.2 versions:

Project B 2.1 depends on project D 1.1;

Project C 1.2 depends on project D 1.3;

In this example, project A’s dependency on project D will conflict, and maven Dependency Mediation will eventually work: Project A will rely on project D1.1 (proximity, depending on the path and dependency, depends on the maven version).

In this case, since project C relies on version 1.3 of project D, but it is actually version 1.1 in effect at run time,

Therefore, it is easy to generate problems at runtime, such as NoSuchMethodError, ClassNotFoundException, etc.

Some JAR conflicts are difficult to locate, and this method can save a lot of time to locate such problems.

Spring, SpringBoot, and SpringCloud themselves all use this mechanism to resolve third-party package conflicts.

Common official BOM:

1) RESTEasy Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-bom</artifactId>
            <version>3.0.6. The Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

2. JBOSS Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>Jboss - javaee - 6.0 - with the other</artifactId>
            <version>${some.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement> 
Copy the code

3) Spring Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.0.1. RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

4) Jersey Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

5) SpringCloud SpringBoot Maven BOM dependency

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

This may seem like a bit of a stretch, but I’ll explain in detail what these configurations do

It is also recommended to use this fine tradition in my own projects. Especially in the early stage of project development, it is difficult to modify BOM in the later stage, which may involve a lot of revisions.

How BOM?

Define the BOM

A BOM is essentially a POM file with the difference that only the

section is in effect for the user.

Just define the release version of the client in

For example, you need to unify all SpringBoot and SpringCloud versions in your project

The first step is to add two official Boms to the POM file. Taking the latest stable SpringBoot version as an example, the combination of official recommended versions is relatively stable, and there will be no major problems generally

<groupId>com.niu.not</groupId>
<artifactId>niu-dependency</artifactId>
<version>1.1.1</version>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.4.6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
</dependencies>
Copy the code

The Gson below is a jar that requires a unified version in addition to SpringBoot and SpingCloud

Other engineering use methods

add BOM under node in project main POm. XML file:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.niu.not</groupId>
            <artifactId>niu-dependency</artifactId>
            <version>1.1.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

In
of the POm. XML file where you need to use the JAR package, import the following:

<dependencies>
    <! Spring and Gson do not need to add the version number, the version provided in the BOM will be automatically referenced -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>
</dependencies>
Copy the code

After this setting, if the project requires an upgrade to the Spring version, you only need to verify compatibility with the provider upgrade, and then modify the BOM dependency

If you need to use a different version of the JAR package that is maintained in the current BOM, you can add

to overwrite it, as follows:

<dependencies>
    <! Spring and Gson do not need to add the version number, the version provided in the BOM will be automatically referenced -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <! -- Overwrite version 2.8.6 in BOM and use custom version 2.8.2-->
        <version>2.8.2</version>
    </dependency>
</dependencies>
Copy the code

summary

Jar conflicts are very annoying, Spring framework-related conflicts, some of the errors are very unclear or stop the service without any errors at all,

This is a difficult problem to pinpoint, and developers should focus on business development and not waste too much time on it,

So unified versioning is very useful, otherwise Spring’s awesome framework is in use,

BOM management, bring it to you!!

Reference: howtodoinjava.com/maven/maven…